10

I wondering what is the best way to convert a timestamp of this format -

2012-02-18 14:28:32

to a date presentation of this format -

Saturday Feb 2012 14:28:32

Many thanks :)

1
  • As for formatting to a final string I could suggest you the following library. As far as the first part of the question is concerned you could refer to the following question Commented Feb 18, 2012 at 18:41

6 Answers 6

6

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/

Sign up to request clarification or add additional context in comments.

Comments

3

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;

Comments

3

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);

2 Comments

??? Uncaught RangeError: toString() radix argument must be between 2 and 36 at Number.toString (<anonymous>) at <anonymous>:2:21
@PaoloBiavati that means you call toString on a number, not a Date: stackoverflow.com/questions/16527109/… (use datejs).
2

I'd suggest using an external js library to do that. To my understanding, Moment.js is the best date-time conversion library out there.

In this case, it does the job in one line. Just add the moment.js in you project and then do

var timestamp = '2012-02-18 14:28:32';
var formattedTime = moment(timestamp).format('dddd MMM YYYY HH:mm:ss'); // Saturday Feb 2012 14:28:32

1 Comment

Not "The Best" when it skips the day-of-month in your example. Or should the format be: dddd MMM dd, YYYY HH:mm:ss? Why to add a library (import and all) just to do something so trivial like printing the date in a formatted form?
1

JavaScripts Date object is lacking methods for formatting. I would consider using an external library like this one. Seems it has what you're looking for.

Comments

0

try this blog it has enough dateformats:

http://blog.stevenlevithan.com/archives/date-time-format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.