Convert ISO 8601 time date into plain English

18,533

Solution 1

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<L;i++){
                day[i]=parseInt(day[i], 10) || 0;
                };
                day[1]-= 1;
                day= new Date(Date.UTC.apply(Date, day));
                if(!day.getDate()) return NaN;
                if(p[5]){
                    tz= (parseInt(p[5], 10)*60);
                    if(p[6]) tz+= parseInt(p[6], 10);
                    if(p[4]== '+') tz*= -1;
                    if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
                }
                return day;
            }
            return NaN;
        }
        // shim implemented;
    }
    else{
        Date.fromISO= function(s){
            return new Date(s);
        }
        //native ISO Date implemented;
    }
})()

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

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

Solution 2

Take a look at the Date object API in JavaScript: http://www.w3schools.com/jsref/jsref_obj_date.asp. This should get you started:

var timeStr = "2010-01-13T18:31:16Z";
var date = new Date(timeStr);
var day = date.getDate();
var year = date.getFullYear();
var month = date.getMonth()+1;
var dateStr = month+"/"+day+"/"+year;

There are methods to get the time, which you can also append to dateStr.

Solution 3

You can easily change the format with an regex, e.g.

return datestring.replace(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z/, "$3/$2/$1 $4:$5:$6");

or, you parse the string to a Date object, and extract the single values (or many more, like the weekday etc):

var date = new Date(datestring); // parses ISO 8601
return date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+" "+…
Share:
18,533

Related videos on Youtube

user887515
Author by

user887515

Updated on September 14, 2022

Comments

  • user887515
    user887515 over 1 year

    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/yyyy hh:mm:ss.

    Thank you

    • Sebastian Breit
      Sebastian Breit over 11 years
      you could specify a language...
    • user887515
      user887515 over 11 years
      sorry - i had it in the tags but forgot to put in the body.
  • kennebec
    kennebec over 11 years
    Why are you adding a day to the date?
  • Eric Levine
    Eric Levine over 11 years
    @kennebec Good call, that wasn't necessary. I've edited my code.
  • craft
    craft over 5 years
    Reading your comments, I briefly wondered why +1 was added to the month as well, but I see it's off the zero index. thx!