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

I am developing a Node.js application and I have to convert a German string date like am 13. Dezember 2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744445071a4574555.html

相关推荐

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

    I am developing a Node.js application and I have to convert a German string date like am 13. Dezember 2

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信