2015-07-12 246 views
0

我正在使用AngularJS构建html应用程序,其中我想在html页面上显示json文件数据。谁能帮我从JSON文件转换GMT日期格式小时:分钟AM/PM将GMT日期格式转换为时间格式

JSON文件看起来是这样的:

[ 
    { 
     "startTime": "Sun May 24 2015 01:00:00 GMT+0530 (IST)", 
     "endTime": "Sun May 24 2015 01:30:00 GMT+0530 (IST)", 
     "title": "Event 1" 
    }, 
    { 
     "startTime": "Sun May 24 2015 04:00:00 GMT+0530 (IST)", 
     "endTime": "Sun May 24 2015 06:00:00 GMT+0530 (IST)", 
     "title": "Event 2" 
    }, 
    { 
     "startTime": "Sat May 23 2015 20:00:00 GMT+0530 (IST)", 
     "endTime": "Sat May 23 2015 21:30:00 GMT+0530 (IST)", 
     "title": "Event 3" 
    }, 
    { 
     "startTime": "Sat May 23 2015 21:30:00 GMT+0530 (IST)", 
     "endTime": "Sat May 23 2015 22:15:00 GMT+0530 (IST)", 
     "title": "Event 4" 
    }, 
    { 
     "startTime": "Sun May 24 2015 02:00:00 GMT+0530 (IST)", 
     "endTime": "Sun May 24 2015 03:00:00 GMT+0530 (IST)", 
     "title": "Event 5" 
    } 
] 

回答

0

链接通过@wor提供ldask说

日期格式化或者作为日期对象毫秒(字符串或数字)或各种ISO 8601 日期时间字符串格式(例如yyyy-MM-ddTHH:mm:ss.sssZ及其较短版本,如yyyy-MM-ddTHH:mmZ,yyyy-MM-dd或yyyyMMddTHHmmssZ)。如果 没有在字符串输入中指定时区,则时间被认为是 在本地时区。

所以我使用的是自定义过滤器转换Sun May 24 2015 01:00:00 GMT+0530 (IST)milliseconds,然后使用角度滤波器显示为hh:mm AM/PM

HTML

<div ng-repeat="date in dateJson"> 
     {{date.startTime | myDateFilter | date:'hh: mm a' }} 
</div> 

dateJson是你的样品JSON ...

自定义过滤器

app.filter('myDateFilter', function() { 

    return function(dateString) { 
    var date = new Date(dateString) 

    // converting to milliseconds 
    dateString = date.getTime(); 
    return dateString; 
    } 
}); 

SAMPLE PLUNKER

1

您的JSON数据分配给一个范围变量:

scope.data = [ 
    { 
     "startTime": "Sun May 24 2015 01:00:00 GMT+0530 (IST)", 
     "endTime": "Sun May 24 2015 01:30:00 GMT+0530 (IST)", 
     "title": "Event 1" 
    }, 
    ... 
]; 
在你的HTML

,您可以使用angularjs date filter这样的:

<div>{{ data[0] | date: 'h:m a' }}</div> 
+0

用于显示上午/下午您可以使用过滤器,如{{data |日期:'h:m a'}}' –

+0

是的,我错过了,谢谢 – worldask