0


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

Peter
  • 11,413
  • 31
  • 100
  • 152

2 Answers2

0

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));
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-4

use this,

var d1=new Date();
d1.toString('yyyy-MM-dd');       //returns "2009-06-29"
d1.toString('dddd, MMMM ,yyyy')  //returns "Monday, June 29,2009"

you can set your format which you want

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Bhargav Mistri
  • 944
  • 8
  • 20
  • In what browser does date.toString() take parameters??? And the asker wants DDD, dd MMM YYYY hh:mm:ss TZ – mplungjan Aug 04 '11 at 11:51
  • you send that link http://blog.stevenlevithan.com/archives/date-time-format, and you vote down, i think you have disease of sort team memory loss – Bhargav Mistri Aug 04 '11 at 11:56
  • How so? the link I provide use a library and the code would be d.format(....) and not d.toString(....) so what are you talking about? (and I formatted your code for you - use the {} button ) – mplungjan Aug 04 '11 at 12:09