如何用瞬间得到一天的开始和结束?

我想得到的开始和结束当前的一天(和辅助的明天由 .add(1, 'day'))使用 moment

我现在得到的是

now = moment()
console.log('now ' + now.toISOString())
console.log('start ' + now.startOf('day').toISOString())
console.log('end ' + now.endOf('day').toISOString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>

This outputs right now

now 2018-04-18T21:20:02.010Z
start 2018-04-17T23:00:00.000Z
end 2018-04-18T22:59:59.999Z

由于时间移动了一个小时,我相信这与时区有关,尽管我不明白这与时区有什么关系: 不管时区是什么,那个时区的一天从今天午夜后开始,到今天午夜前结束。

140252 次浏览

It is giving you midnight local time, but you're printing it out in zulu time. Try using toString instead, it will print the time out in local time.

now = moment()
console.log('now ' + now.toString())
console.log('start ' + now.startOf('day').toString())
console.log('end ' + now.endOf('day').toString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>

If you want to start with Monday you have to use this one moment().startOf('isoWeek');

console.log({
from_date: moment().startOf('isoWeek').toString(),
today: moment().toString(),
to_date: moment().endOf('isoWeek').toString(),
});