Date format conversion in JavaScript

Javascript date functions are pretty bad... You have the option to convert to UTC http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_toutcstring

But if it was me, i would look into Datejs: http://www.datejs.com/ best javascript date api for me

Please take a look at the getting started with Datejs: http://www.datejs.com/2007/11/27/getting-started-with-datejs/


You must first define an array of the English words (Sunday, Monday, Feb, Mar, etc.):

var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
    monthsOfYear = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

To be able insert the extra 0 at the beginning of the minutes and seconds, define a padding function for the String prototype:

String.prototype.padLeft = function(padString,length){
    var toReturn = String(this);
    while(toReturn.length < length){
        toReturn = padString + toReturn;
    }
    return toReturn;
}

Format the date and time like this:

var time = new Date(), formattedDate, formattedTime, wholeThing;
formattedDate = daysOfWeek[time.getDay()] + ", " + monthsOfYear[time.getMonth()] + " " + time.getDate() + ", " + time.getFullYear();
formattedTime = time.getHours() + ":" + time.getMinutes().padLeft("0",2) + time.getSeconds().padLeft("0",2);

You can get the whole thing by concatenating formattedDate and formattedTime, as in:

wholeThing = formattedDate + " " + formattedTime;

Consider using datejs which is rocks!

var mydate = Date.parse('2012-02-18 14:28:32');
var result = mydate.toString('dddd MMM yyyy h:mm:ss');
console.log(result);