简体   繁体   中英

moment.js convert to ISO output null + warning on specific date

I am using "moment": "2.22.2" in my React application

and I have two strings dates, when i convert them using moment().toISOString() one of them are return null.

null is return for all dates newer than 13.11.2019

for exam: 14.11.2019 & 15.11.2019 &16.11.2019 ...

    const date1 = '12.11.2019 23:55';
    const date2 = '13.11.2019 23:55';

    moment(date1).toISOString() // =>  2019-12-11T22:55:00.000Z
    moment(date2).toISOString() // =>  null

is it something wrong locally in my application ? or it is an error because of moment library ?

I think you should parse your strings using a custom format since without it moment thinks 12.11 is the 11 of december (in the form [month].[day]).

use this form instead:

const date1 = '12.11.2019 23:55';
const date2 = '13.11.2019 23:55';

moment(date1,'DD.MM.YYYY HH:mm').toISOString()
moment(date2,'DD.MM.YYYY HH:mm').toISOString()

See docs here ( https://momentjs.com/docs/#/parsing/string-format/ )

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