最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert a German date to ISO date - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Jan 12, 2018 at 9:19 VincenzoC 31.5k12 gold badges100 silver badges121 bronze badges asked Jan 12, 2018 at 8:31 devvdevv 671 silver badge6 bronze badges 1
  • 3 Wele to Stack Overlow. In order to help you, you must clarify what are the versions of libraries used and what you tried so far with a Minimal, Complete, and Verifiable example: there are a lot of reasons for an invalid date error in momentjs (format, locale, strict mode) – Lex Lustor Commented Jan 12, 2018 at 8:51
Add a ment  | 

4 Answers 4

Reset to default 4

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() and moment.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./ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>

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();

Have a look here:

https://momentjs./docs/

Do you need something like that:

moment('13. Dezember 2017', 'DD. MMMM YYYY', 'de').format(moment.ISO_8601);

You can easily format it using:

var now = moment(); // pass your date to moment
var formatedNow = moment().toISOString(now);
or
var formatedNow = now.toISOString();
发布评论

评论列表(0)

  1. 暂无评论