momentjs startOf does not work on existing date object

后端 未结 1 1531
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 15:17

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()).startO         


        
1条回答
  •  星月不相逢
    2021-01-15 15:44

    I suggest to use format() to display the value of a moment object.

    As the Internal Properties guide states:

    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()

    Here a snippet showing the correct results:

    console.log(moment().startOf('day').format());
    console.log(moment(new Date()).startOf('day').format());

    0 讨论(0)
提交回复
热议问题