gpt4 book ai didi

momentjs startOf 不适用于现有的日期对象

转载 作者:行者123 更新时间:2023-12-04 03:05:38 26 4
gpt4 key购买 nike

我正在尝试使用时刻来开始新的一天。我得到以下不同的结果:

moment().startOf('day'); //good
moment(new Date()).startOf('day'); //this does not work!

fiddle : https://jsfiddle.net/y1of77wx/

实际情况是我在一个将日期对象作为参数的函数中执行此操作:
function doWork(dt) {
return moment(dt).startOf('day');
}

我确定解决方案很简单,但我只是遗漏了一些东西。

最佳答案

我建议使用 format() 显示矩对象的值。

Internal Properties指南指出:

Moment objects have several internal properties that are prefixed with _.

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.

...

To print out the value of a Moment, use .format(), .toString() or .toISOString()



这是一个显示正确结果的片段:

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 不适用于现有的日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com