我想确定Moment.js中给定日期的一周的第一天。这是我的代码:
var begin = moment("2015-08-15").startOf('week').isoWeekday(0);
console.log(begin);
var end = moment("2015-08-15").endOf('week').isoWeekday(0);
console.log(end);
我得到以下输出:
Object { _isAMomentObject: true, _i: "2015-08-15", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2015-08-01T22:00:00.000Z }
Object { _isAMomentObject: true, _i: "2015-08-15", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2015-08-09T21:59:59.999Z }
根据文件,当地人是' en'默认情况下,这意味着一周的第一天是星期日。在我看来,正确的结果应该是:
begin = 2015-08-09
end = 2015-08-15
当我从' 0'更改isoWeekday
时到' 1'结果如下:
Object { _isAMomentObject: true, _i: "2015-08-15", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2015-08-02T22:00:00.000Z }
Object { _isAMomentObject: true, _i: "2015-08-15", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object, _locale: Object, _d: Date 2015-08-10T21:59:59.999Z }
这也是错误的。正确答案是:
begin = 2015-08-10
end = 2015-08-16
答案 0 :(得分:3)
正确的答案是:
var begin = moment().subtract(1, 'week').startOf('week').format("YYYY MM DD");
var end = moment().subtract(1, 'week').endOf('week').format("YYYY MM DD");
感谢@MattJohnson的帮助
答案 1 :(得分:1)
正在进行的是您在2015-08-15的当地周(即2015-08-09周日)获得startOf()
,然后您重置 ISO周 ,即周一至周日,第一天,也就是2015-08-02周一。
您需要使用startOf('isoWeek')
来保持一致行为。