Use JavaScript to Parse Time

前端 未结 4 492
春和景丽
春和景丽 2021-02-04 14:13

This is probably something easy, but I\'m a bit confused how to do this. How can I, using JavaScript, parse only the time from the following ISO 8601 date string:



        
相关标签:
4条回答
  • 2021-02-04 14:25

    Parsing the ISO timestamp is easy, formatting the time in a culturally appropriate way is hard (5:10 PM is not appropriate for all locales) Many toolkits provide routines for the ISO part, and it's even part of the new ECMAScript 5 standard; only a couple do the latter part, however.

    You can try dojo.date.stamp.fromISOString and dojo.date.locale.format.

    I believe Date-JS can format times also.

    0 讨论(0)
  • 2021-02-04 14:35

    string.slice(11,16) will return 17:10. From there (possibly using slice in more interesting and exciting manners) it should be fairly simple to get it into 24-hour format.

    0 讨论(0)
  • 2021-02-04 14:35

    This is so called ISO 8601 format. Mochikit comes with a function to parse it,

    http://mochikit.com/doc/html/MochiKit/DateTime.html

    You can get a Date object like this,

      timestamp = isoTimestamp("2009-12-06T17:10:00");
    

    Just copy the function if you don't want use Mochikit.

    0 讨论(0)
  • 2021-02-04 14:45

    Chrome & Firefox: Standard JavaScript Date constructor takes ISO 8601 date string. For example:

    var sampleDate = new Date("2010-03-07T02:13:46Z");

    Returns this Date object: "Sun Mar 07 2010 13:13:46 GMT+1100 (AUS Eastern Daylight Time)"

    This does not work in IE (including latest IE 9)

    Here is a cross-browser solution by Paul Sowden at http://delete.me.uk/2005/03/iso8601.html :

    Date.prototype.setISO8601 = function (string) {
        var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
            "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
            "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
        var d = string.match(new RegExp(regexp));
    
        var offset = 0;
        var date = new Date(d[1], 0, 1);
    
        if (d[3]) { date.setMonth(d[3] - 1); }
        if (d[5]) { date.setDate(d[5]); }
        if (d[7]) { date.setHours(d[7]); }
        if (d[8]) { date.setMinutes(d[8]); }
        if (d[10]) { date.setSeconds(d[10]); }
        if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
        if (d[14]) {
            offset = (Number(d[16]) * 60) + Number(d[17]);
            offset *= ((d[15] == '-') ? 1 : -1);
        }
    
        offset -= date.getTimezoneOffset();
        time = (Number(date) + (offset * 60 * 1000));
        this.setTime(Number(time));
    }
    

    Usage:

    var date = new Date();
    date.setISO8601("2005-03-26T19:51:34Z");
    

    If you do a lot of datetime manipulation in JavaScript, I can also suggest to check some JS libraries like MomentJS. It handles some common things like date parsing, formatting and calculating difference between two dates and has support for multiple localizations.

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