我有两个日期-postDelayed()
和date_to = new Date()
应该是
date_from
粒度可以是小时,日,周和月
到目前为止,我有这些常量
date_from = date_to - 10 * granularity units
我这样改变日期
const MS_PER_DAY = 1000 * 60 * 60 * 24;
const MS_PER_HOUR = 1000 * 60 * 60;
const MS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;
但是我现在确定如何通过给定的公式获得第二个约会。 所有帮助将不胜感激。
答案 0 :(得分:2)
您几乎拥有它:
date_from = new Date(date_to.getTime() - 10 * granularity);
getTime
返回毫秒数,因为您的时间是毫秒,并且您的粒度值以毫秒为单位,当您将数字传递到new Date
时,它将用作毫秒数。 (从技术上讲,您不需要getTime
调用,因为在减法表达式中使用日期会触发其valueOf
方法,对于Dates,它与getTime
相同。但是为了清楚起见。 ..)
示例:
const MS_PER_DAY = 1000 * 60 * 60 * 24;
const MS_PER_HOUR = 1000 * 60 * 60;
const MS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;
const date_to = new Date(2018, 8, 1); // Sep 1 2018
const granularity = MS_PER_DAY;
const date_from = new Date(date_to.getTime() - 10 * granularity);
console.log("date_to: " + date_to.toISOString());
console.log("granularity: MS_PER_DAY");
console.log("date_from: " + date_from.toISOString());