How can I convert string to datetime with format specification in JavaScript?

前端 未结 15 1026
终归单人心
终归单人心 2020-11-22 02:35

How can I convert a string to a date time object in javascript by specifying a format string?

I am looking for something like:

var dateTime = convert         


        
15条回答
  •  一生所求
    2020-11-22 03:15

    Just to give my 5 cents.

    My date format is dd.mm.yyyy (UK format) and none of the above examples were working for me. All the parsers were considering mm as day and dd as month.

    I've found this library: http://joey.mazzarelli.com/2008/11/25/easy-date-parsing-with-javascript/ and it worked, because you can say the order of the fields like this:

    >>console.log(new Date(Date.fromString('09.05.2012', {order: 'DMY'})));
    Wed May 09 2012 00:00:00 GMT+0300 (EEST)
    

    I hope that helps someone.

提交回复
热议问题