问题描述
我正在尝试利用瞬间来开始新的一天.我得到以下不同的结果:
I'm trying to use moment to get the start of a day. I get different results with the following:
moment().startOf('day'); //good
moment(new Date()).startOf('day'); //this does not work!
小提琴: https://jsfiddle.net/y1of77wx/
实际情况是,我在将日期对象作为参数的函数中执行此操作:
The practical case is that I'm doing this in a function that takes in a date object as an argument:
function doWork(dt) {
return moment(dt).startOf('day');
}
我确定解决方案很简单,但是我只是缺少一些东西.
I'm sure the solution is simple but I'm just missing something.
推荐答案
我建议使用 format()
以显示矩对象的值.
I suggest to use format()
to display the value of a moment object.
内部属性指南指出:
最常查看的内部属性是_d
属性,其中包含Moment包装器的JavaScript Date.通常,开发人员会对控制台输出_d
的值感到困惑.
The most commonly viewed internal property is the _d
property that holds the JavaScript Date that Moment wrappers. Frequently, developers are confused by console output of the value of _d
.
...
要打印出Moment的值,请使用.format()
,.toString()
或.toISOString()
To print out the value of a Moment, use .format()
, .toString()
or .toISOString()
以下是显示正确结果的代码段:
Here a snippet showing the correct results:
console.log(moment().startOf('day').format());
console.log(moment(new Date()).startOf('day').format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
这篇关于momentjs startOf不适用于现有的日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!