使用 Moment.js 清除浏览器 native 日期对象时间的最佳方法是什么,以便在我浏览器的当前时区是今天午夜。
最佳答案
最接近你所问的是:
var d = new Date(); // your browser native date object
var m = moment(d).startOf('day'); // move to the start of the day in the local time zone
d = m.toDate(); // convert back to a native date object
但是,如果这是您唯一想做的事情,则不需要 Moment。只是做:
d = d.setHours(0, 0, 0, 0);
另外,请记住,并非每个地方日都有午夜。如果在 00:00 有 DST“向前”过渡,则一天从 01:00 开始。这种情况发生在巴西和世界其他几个地方。在这种情况下,Moment 的 startOf('day')
函数将确保结果为 1:00,但是 setHours
方法可能设置为 1:00,或者可能设置为 23:前一天为 00,具体取决于浏览器的实现。
关于javascript - 使用 Moment.js 清理时间的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269868/