admin管理员组

文章数量:1292698

I have a date/time displayed using "new date()".

It currently displays

"Thu May 31 2012 13:04:29 GMT-0500 (CDT)".

I need this:

 "Thu May 31 13:04:29 CDT 2012". 

How do I format it?

I have a date/time displayed using "new date()".

It currently displays

"Thu May 31 2012 13:04:29 GMT-0500 (CDT)".

I need this:

 "Thu May 31 13:04:29 CDT 2012". 

How do I format it?

Share Improve this question edited Mar 12, 2014 at 10:01 Vidya asked Mar 12, 2014 at 9:55 VidyaVidya 6983 gold badges12 silver badges21 bronze badges 5
  • What do you mean by zzz? – Ashoka Mondal Commented Mar 12, 2014 at 9:57
  • So here zzz is CDT ? – Priyank Patel Commented Mar 12, 2014 at 10:00
  • Check github./phstc/jquery-dateFormat – Ashoka Mondal Commented Mar 12, 2014 at 10:04
  • 2 How do I do without a plugin sir? – Vidya Commented Mar 12, 2014 at 10:08
  • 1 JavaScript native date/time handling is still very primitive. You would be better off using a plugin than reinventing the wheel. I suggest using Moment.js. – Taylor Buchanan Commented Feb 5, 2015 at 7:21
Add a ment  | 

5 Answers 5

Reset to default 1

You can use a regular expression to extract the timezone from the standard date string.

var d            = new Date();
var customFormat = d.toString().slice(0,7) + ' ' +              //Day and Month
                   d.getDate() + ' ' +                          //Day number
                   d.toTimeString().slice(0,8) + ' ' +          //HH:MM:SS
                   /\(.*\)/g.exec(d.toString())[0].slice(1,-1)  //TimeZone
                   + ' '  + d.getFullYear();                    //Year
var a = new Date();
    var fp = a.toDateString().substring(0, a.toDateString().length - 4);
    var sp = a.toLocaleTimeString();
    var tp = a.toDateString().substr(a.toDateString().length - 5);
    $('.timer').html(fp + ' ' + sp + ' ' + tp);

Provided the string always has the format in your example:

var s = "Thu May 31 2012 13:04:29 GMT-0500 (CDT)";
var a = s.split(/ /);

s = a[0] + " " + a[1] + " " + a[2] + " " + a[4] + " " + a[6].substring(1, a[6].length - 1) + " " + a[3];

The moment.js library is great for formatting dates and times. http://momentjs./

Ex: moment().format('MMMM Do YYYY, h:mm:ss a'); // July 14th 2015, 9:29:52 am

// timeStamp  EEE MMM d HH:mm:ss z yyyy
const timeArr = new Date().toString().split('+')[0].split(' ');
const timeStamp = timeArr.slice(0, 3).concat(timeArr.slice(4), timeArr[3]).join(' ');

timeArr creates an array split by space. The second line re-arrange the order of the array to achieve this format => (EEE MMM dd HH:mm:ss zzz yyyy) by using slice and concat function. Lastly, use join to convert it back to string.

本文标签: javascriptFormat a new Date() to EEE MMM dd HHmmss zzz yyyyStack Overflow