Search code examples
javascriptdatemomentjsdata-conversion

Converting string to standard moment format


I have a date in the following string format: "Sunday, 08. Dec. 2019", and I want to convert it to the following format: "2019-12-08T00:00:00.000+01:00".

I want to do that in order to eventually compare the date with another date in the second format I cited, with isBefore().

According to the docs, if you do moment().format() you get the current time in that format, but I couldn't find an example for converting to it.

I tried a few things but can't find the right syntax.

moment("Sunday, 08. Dec. 2019").format(); // Invalid date
moment.utc("Sunday, 08. Dec. 2019").format(); // Invalid date

What would be the accurate syntax? And what is the name of this format "2019-12-08T00:00:00.000+01:00"? It seems to be the default with moment, but I can't find what is the name of it.


Solution

  • You should identify your input format first:

    moment("Sunday, 08. Dec. 2019", "dddd, DD. MMM. YYYY").format();