我有一个变量,它的值为2017-11-10,并希望在3天前从2017-11-10 (即2017-11-7 )获得
似乎只有在你有整个时间戳的情况下才能起作用。然而,我从时间戳中删除了时间。
let d = new Date();
d.setDate(d.getDate()-1);没有时间就能做到这一点吗?
发布于 2017-11-14 02:52:32
以下是获得yyyy格式日期字符串的几个选项:
var d = new Date( new Date("2017-11-10" + "z") - 3 * 864e5 ) // specify Zulu time zone to avoid conversion to local time
console.log( d.toJSON().slice(0, 10) ) // "2017-11-07"
console.log( d.toJSON().slice(0, 10).replace('-0', '-') ) // "2017-11-7"
console.log( d.getUTCFullYear() + '-' + (d.getUTCMonth() + 1) + '-' + d.getUTCDate() ) // "2017-11-7"
可以将时区信息添加到日期字符串中,以避免转换到本地时间:
console.log( new Date("2000-1-2") ) // "2000-01-01T05:00:00.000Z" ("T05:" because converted to local time)
// correct "2000-01-01T00:00:00.000Z"
console.log( new Date("2000-01-02") )
console.log( new Date("2000-1-2 Z") )
console.log( new Date("2000-1-2 GMT") )
console.log( new Date("2000-1-2 UTC") )
console.log( new Date("2000-1-2 z-1234") ) // "2000-01-02T12:34:00.000Z" ha ..
发布于 2017-11-14 02:02:25
您可以将日期字符串插入到javascript日期,如下例所示
let d = new Date(2017, 10, 10);
d.setDate(d.getDate()-3);https://stackoverflow.com/questions/47276456
复制相似问题