Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a new Date() to EEE MMM dd HH:mm:ss zzz yyyy

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?

like image 783
Vidya Avatar asked Mar 12 '14 09:03

Vidya


1 Answers

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
like image 121
Ortomala Lokni Avatar answered Oct 19 '22 15:10

Ortomala Lokni