简体   繁体   中英

Converting a timestamp into more readable format in Javascript

I'm starting off with something that looks like this: 1397773800000

I convert it into something more readable with this:

var time = 1397773800000;
var date = new Date(time);
alert(date); //Thu Apr 17 2014 18:30:00 GMT-0400 (EDT)

However, I want it to be even more readable. I want it to say Thursday, April 17th at 4:30pm EST, for example. And I'm not sure how to separate out each of those components using javascript.

Here's my JSFiddle: http://jsfiddle.net/w4Nbk/

UPDATE: I'm trying out moment.js thanks to a responder, but I can't get it to work in a test JSFiddle: http://jsfiddle.net/w4Nbk/2/

UPDATE #2: Never mind, got it working here: http://jsfiddle.net/yS4da/637/ Could just be that moment.js wasn't called correctly in the fiddle.

I highly recommend you check out moment.js

It's the perfect tool for all things related to dates and times!


var time = 1397773800000;

moment(time).format('MMMM Do YYYY, h:mm:ss a');
moment(time).format('dddd');
moment(time).format("MMM Do YY");
moment(time).format('YYYY [escaped] YYYY');
moment(time).format();

Output

April 17th 2014, 5:30:00 pm
Thursday
Apr 17th 14
2014 escaped 2014
2014-04-17T17:30:00-05:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM