简体   繁体   中英

Moment.js doesn't recognize ISO date format

While adding a new item to my database I'm creating a new date to it by:

const item = new Item({
    author,
    isOrdered,
    name,
    createdAt: new Date(Date.now()).toISOString()
  });

When sorting items, the moment.js is called and it warns me in the console

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

The date format that I'm saving to db is 2019-01-09T07:55:34.665Z . I've been reading the documentation and by following the instructions I did something like this, to specify the date format: moment('2019-01-09T07:55:34.665Z', 'ddd, D MMM YYYY H:m:s Z') but still, it throws me a warning. How to workaround this warning?

try to specify the format separately like,

moment('2019-01-09T07:55:34.665Z').format('ddd, D MMM YYYY H:m:s Z')

'Wed, 9 Jan 2019 15:55:34 +08:00'

I would prefer to see the database applying the timestamp. This would depend a bit on what db and potentially orm you're using.

In javascript, as mentionned in the comments, just do...

createdAt : new Date().toISOString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM