0

I have a timestamp that looks like this: "2012-12-29T20:00:00Z". What is the best way to convert that to month and date? Something like Dec 29.

3 Answers 3

1

I don't think there is a simple way to do so. but there are some libraries for this purpose. Like this one

http://momentjs.com/

And if you don't care about localization problem, just use internal Date object's prototype.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

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

7 Comments

I would use the Date object, but I'm being given this timestamp by an API so I have to convert it one way or another.
You mean from ISO date to internal date type? new Date('2012-12-29T20:00:00Z') will parse it.
Sorry, so how would I use that object to convert it to my preferred format? Say Month Date?
Use a library, as Date object will tell you which month it is (in your case 12) but not 'December', as 12th month's name varies from locales to locales you cannot retrieve it from Date object.
Or you can use a simple array mapping, like in my answer. No need for a library if you want such a simple thing.
|
0

Use the built-in Date object.

// Date.parse parses an ISO date
var d = new Date( Date.parse( '2012-12-29T20:00:00Z' ) );

console.log( d.toString() ); // "Sat Dec 29 2012 21:00:00 GMT+0100 (Paris, Madrid)"

console.log( d.toString().substr( 4, 6 ) ); // "Dec 29"

Note that substr will always work since days always have 3 characters, months always have 3 and dates always have 2.

If you don't want to use substr, there is no way to get "Dec" straight out. You can play with a mapping array however, like the following:

console.log( d.getMonth() ); // 11

var mapping = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];

console.log( mapping[ d.getMonth() ] + ' ' + d.getDate() ); // "Dec 29"

This takes advantage of the fact that an array's index starts at 0, just like d.getMonth().

For IE8 support of Date.parse, I suggest you use the following shim (from ES5-shim): https://gist.github.com/303249

6 Comments

Thanks. I like this approach because it's lightweight and I don't want to include a whole library for it. Will this work in IE? If not, is there an approach that will?
It works in IE, there is no reason it shouldn't. Arrays and indexes are the same everywhere.
Hmm, okay. But @panda-34 said "parsing ISO dates happens only in ecma version 5, which means IE 8 doesn't support it"?
Oh, this part. Let me think about it and come back to you.
@OliverJosephAsh Edited to add Date.parse and a shim that will make it work everywhere :-)
|
0

You can convert the string to a Date Object using new Date("2012-12-29T20:00:00Z") which in my timezone results in Sat Dec 29 2012 21:00:00 GMT+0100 (W. Europe Standard Time). There are some datatime frameworks to pick from for formatting that, like datejs. I've cooked up a simple one in this jsfiddle, may be a starting point for you. I've added your ISO-date to the examples. It also works for older browsers.

2 Comments

Do you really need a framework when a mapping array is enough? ;o)
Not if you only want Dec 29 from a date string. I was just pointing out the possibilities

Your Answer

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