How to parse a string into a date object at JavaScript?

后端 未结 2 1688
一整个雨季
一整个雨季 2020-12-02 02:52

How to parse a string into a date object at JavaScript (without using any 3d party) that is at dd-MM-yyyy HH:mm (all of them numbers) format?

相关标签:
2条回答
  • 2020-12-02 03:24
    var p = "04-22-1980 12:22".split(/-|\s+|:/);
    // new Date(year, month, day [, hour, minute, second, millisecond ])
    new Date(p[2], p[0] - 1, p[1], p[3], p[4]);
    // => Tue Apr 22 1980 12:22:00 GMT-0500 (Central Daylight Time)
    
    0 讨论(0)
  • 2020-12-02 03:35

    DateJS is your friend: http://www.datejs.com/

    It parses pretty much anything reasonable you throw at it:

    // Convert text into Date
    Date.parse('today');
    Date.parse('t + 5 d'); // today + 5 days
    Date.parse('next thursday');
    Date.parse('February 20th 1973');
    Date.parse('Thu, 1 July 2004 22:30:00');
    

    It's not perfect, but it does a pretty good job.

    0 讨论(0)
提交回复
热议问题