gpt4 book ai didi

javascript - Moment() 与预定义的 Moment() 日期

转载 作者:行者123 更新时间:2023-11-30 07:55:59 26 4
gpt4 key购买 nike

我对 Moment 函数的工作原理有点困惑(例如 startOf()weekday() 等)。

我的问题是:如何从预定义的日期获取一周的开始?

考虑 9/21/16 上的这两个调用:

moment().startOf('week') // Returns a moment object with date set to Sun Sep 18 2016 ...

对比

moment(new Date("9/21/16")).startOf('week') // Returns a moment object with date set to Wed Sep 21 2016 ...

为什么它们的行为方式不同?使用 weekday(0) 而不是 startOf('week')

时也是如此

最佳答案

您不能简单地将 Date 构造函数与您喜欢的任何日期格式一起使用。例如,在我使用 Firefox 的系统上,new Date("9/21/16") 返回一个设置为 1916-09-20T22:00:00.000Z 的 Date 对象.

使用 ISO 8601格式,甚至更好,parse your string with Moment itself :

moment("9/21/16", "MM/DD/YY")

关于javascript - Moment() 与预定义的 Moment() 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620655/

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