简体   繁体   中英

How do I convert API supplied date/time string into readable format?

I'm working with a Python backend API that gives me date/time in a string represented as such:

2014-07-21T16:50:34.144Z  

What is this format, and how might I use javascript in my Ember app to put that string into a human readable format?

Well, simply:

var string_date = '2014-07-21T16:50:34.144Z'
var date = new Date(string_date);

date.getDate() // returns 21 -> day
date.getMonth() // returns 6, note it's start counting from 0, so 0 is January.

for more info: Date object reference

Ember actually includes a library( http://momentjs.com/ ) for handling dates/times. I created a helper with the following code:

Ember.Handlebars.helper('format-date', function(date) {
  return moment(date).format('LLL');
});

Then in my handlebars template I have a call to the helper:

{{format-date dateCreated}}

The helper tag passes the date attribute to the helper, and the moment function handles the formatting. The 'LLL', per the moment.js docs specifies the actual format the date takes. In my case

2014-07-21T16:50:34.144Z  

formats into

July 21 2014 9:50 AM

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