Convert dd.mm.yyyy format to yyyy-mm-dd

后端 未结 5 1483
孤街浪徒
孤街浪徒 2020-12-16 05:24

How can I convert dd.mm.yyyy format date to yyyy-mm-dd format in JavaScript?

Here is an example:

30.01.2010
to 
2010-01-30

Meaning

5条回答
  •  感情败类
    2020-12-16 05:42

    Datejs can parse that. The code is at http://datejs.googlecode.com/files/date.js

    EDIT: It is not safe to left date.js determine the format string automatically. I made the mistake of not testing with a day <= 12 (duh). You should use:

    Date.parseExact('09.01.2010', 'd.M.yyyy').toString('yyyy-MM-dd');
    

    or

    Date.parseExact('09.01.2010', 'dd.MM.yyyy').toString('yyyy-MM-dd');
    

    depending on whether you want to allow single digit days.

提交回复
热议问题