【发布时间】:2020-09-27 06:33:18
【问题描述】:
以下脚本计算我下周五和下周日的日期。
问题:使用 .toISOString 使用 UTC 时间。我需要改变输出当地时间的东西。我对 javascript 很陌生,所以我找不到合适的属性来代替 .toIsostring。 我该怎么办?
function nextWeekdayDate(date, day_in_week) {
var ret = new Date(date || new Date());
ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
return ret;
}
let nextFriday = nextWeekdayDate(null, 5);
let followingSunday = nextWeekdayDate(nextFriday, 0);
console.log('Next Friday : ' + nextFriday.toDateString() +
'\nFollowing Sunday: ' + followingSunday.toDateString());
/* Previous code calculates next friday and next sunday dates */
var checkinf = nextWeekdayDate(null, 5);
var [yyyy, mm, dd] = nextFriday.toISOString().split('T')[0].split('-');
var checkouts = nextWeekdayDate(null, 7);
var [cyyy, cm, cd] = followingSunday.toISOString().split('T')[0].split('-');
【问题讨论】:
-
如果您还没有查看过
Date.getTimezoneOffset方法,您可能会发现它很有用。它“以分钟为单位返回从当前区域设置(主机系统设置)到 UTC 的时区差异。”您可以使用此信息在将日期对象转换为字符串之前(或将其保存到数据库等之前)相应地调整日期对象developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
与您的other question 一样,您的问题是how to format a date。在这种情况下,您可以使用
nextFriday.toLocaleString('en-CA', {year:'numeric',month:'2-digit',day:'2-digit'})。
标签: javascript date localdate localtime toisostring