Convert ISO 8601 time date into plain English

后端 未结 3 1102
孤街浪徒
孤街浪徒 2020-12-16 03:56

How can I manipulate a date time in the format below (I believe it is ISO 8601 format) with JavaScript?

2010-01-13T18:31:16Z

I\'d like to output as dd/mm/yyy

3条回答
  •  青春惊慌失措
    2020-12-16 04:55

    If you really want plain English, why not convert the string to a Date and call date.toLocaleString(), or toUTCString() if you want GMT.

    var time=new Date('2010-01-13T18:31:16Z').toLocaleString();

    If you want to support IE8 and older browsers you'll need translate the string:

    (function(){
        var D= new Date('2011-06-02T09:34:29+02:00');
        if(!D || +D!==1307000069000){
            Date.fromISO= function(s){
                var day, tz,
                rx=/^(\d{4}\-\d\d\-\d\d([tT][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
                p= rx.exec(s) || [];
                if(p[1]){
                    day= p[1].split(/\D/);
                    for(var i=0,L=day.length;i

    var time= Date.fromISO('2010-01-13T18:31:16Z').toLocaleString();

    returned value: (String) Wednesday, January 13, 2010 1:31:16 PM

提交回复
热议问题