问题
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(dateTo).format('YYYY-MM-DD[T]HH:mm:ss');
The date to is 23/04/2105 but the IsoDateTo is returning something like this: 2105-03-04T00:00:00 Also when i enter a date greater than 12 it returns me Invalid Date. Why is this happening?
回答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');
回答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');
来源:https://stackoverflow.com/questions/43848300/moment-format-returns-invalid-date