Search code examples
javascriptjquerydatetimestampmicrotime

Jquery - How can i create a datetime in microformat



what ist the fastest way to create this date format with javascript/jquery?

Wed, 03 Aug 2011 15:49:22 -0700 

Thanks in advance!
Peter


Solution

  • Similar questions have been asked before For example How to convert dateTime format in javascript which links to the useful http://blog.stevenlevithan.com/archives/date-time-format

    To answer this in plain JS, here is something that will give you the date in local time, i.e. your -0700 becomes +0200 on my computer

    http://jsfiddle.net/mplungjan/HMkLd/

    function getMicroformat(date) {
      var d = date.toString();    
      var parts = d.split(" ");
      return parts[0]+", "+
             parts[2]+" "+
             parts[1]+" "+
             parts[3]+" "+
             parts[4]+" "+
             parts[5].replace(/[A-Z]/g,"");
    }
    
    // OR
    
    function getMFReg(d) {
      var reg = /(\D{3}) (\D{3}) (\d{2}) (\d{4}) ([:|\d]{8}) \D{3}([+|-|\d]{3})/
      return d.toString().replace(reg,"$1, $3 $2 $4 $5 $6")
    }
    
    var str = "Wed, 03 Aug 2011 15:49:22 -0700";
    var d = new Date(str);
    document.write(str +"<br>=><br>"+getMicroformat(d));
    document.write("<br>=><br>"+getMFReg(d));