简体   繁体   English

如何将API提供的日期/时间字符串转换为可读格式?

[英]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: 我正在使用Python后端API,该API以这样的字符串形式给我日期/时间:

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? 这种格式是什么?我如何在Ember应用程序中使用javascript将该字符串转换为可读格式?

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. Ember实际上包含一个用于处理日期/时间的库( http://momentjs.com/ )。 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. helper标签将date属性传递给helper,而moment函数则处理格式。 The 'LLL', per the moment.js docs specifies the actual format the date takes. 每时每刻,“ LLL” .js文档会指定日期采用的实际格式。 In my case 就我而言

2014-07-21T16:50:34.144Z  

formats into 格式化成

July 21 2014 9:50 AM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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