Skip to content Skip to sidebar Skip to footer

Moment Format Returns Invalid Date

i have a date which i formated using moment to be shown like this: 03/04/2105. I want to transform it to iso using moment again. As a result i'm writing: const IsoDateTo = moment(d

Solution 1:

To make sure that you are correctly parsing the string you want to pass the expected string format along to the momentjs (something like this):

const IsoDateTo = moment(dateTo,'DD/MM/YYYY').format('YYYY-MM-DD[T]HH:mm:ss');

Solution 2:

You cannot just throw any date format into it and expect it to magically recognize the format. Moment.js relies on the date parsing functionality of JavaScript if you do not specify and other format. According to the MDN specification of Date, "dateString" can be either IETF-compliant RFC 2822 timestamps or a version of ISO8601. Your date string is neither of it.

It is usually the best to use a date format like YYYY-MM-DD.

const IsoDateTo = moment('2105-03-04').format('YYYY-MM-DD[T]HH:mm:ss');

Post a Comment for "Moment Format Returns Invalid Date"