Reputation: 67
I am developing a Node.js application and I have to convert a German string date like am 13. Dezember 2017
to ISO Date and when I used moment.js library to convert it I got an invalid date
, any solutions?
Upvotes: 3
Views: 6552
Reputation: 31482
You can parse 13. Dezember 2017
using moment(String, String, String)
and then use toISOString()
.
Since your input is neither in ISO 8601 recognized format, neither in RFC 2822 you have to provide format parameter. DD
stands for day of the month, MMMM
stands for month's name and YYYY
stands for 4 digit year.
The third parameter tells moment to parse input using given locale:
As of version 2.0.0, a locale key can be passed as the third parameter to
moment()
andmoment.utc()
.
Note that you have to import de
locale to make it work (using moment-with-locales.js
or /locales/de.js
in browser or following Loading locales in NodeJS section for node).
Here a live example:
var m = moment('13. Dezember 2017', 'DD MMMM YYYY', 'de');
console.log( m.toISOString() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>
Upvotes: 4
Reputation: 1089
You can easily format it using:
var now = moment(); // pass your date to moment
var formatedNow = moment().toISOString(now);
or
var formatedNow = now.toISOString();
Upvotes: 0
Reputation: 508
Have a look here:
Do you need something like that:
moment('13. Dezember 2017', 'DD. MMMM YYYY', 'de').format(moment.ISO_8601);
Upvotes: 0
Reputation: 2698
You can use the toISOString method, it return a Date object as a String, using the ISO standard:
var d = new Date();
var n = d.toISOString();
Upvotes: 0