我知道这个问题有几个很好的质量答案,但我正在努力使用它的语法:
$.getJSON( "ticketweb.json",
function(data){
$.each(data.events, function(){
$('ul#listings').append("<li><div class=event-col><span class=name>" +this.eventname+ "</span><p>" +this.eventurl+ "</p><div class=date>" +this.dates.startdate+ "</div><p>" +this.eventimages+ "</p><p>" +this.venue.name+ "</p></div></li>");
});
});
其中
<div class=date>" +this.dates.startdate+ "</div>
.append的一部分是我想要格式化的位。我尝试了很多方法,但不确定如何将更通用的json格式方法合并到上面的语法中。
编辑:日期格式我理想情况下将是:11月17日星期六,虽然17.11.12也可以。
当前获得的输出是20120629100000.没有像我在类似问题上看到的前向或反斜杠(/ Date(1224043200000)/),我不确定是否有一个良好编码实践的元素我'我在这里失踪了。我的jSON文件在下面输入完全有效:
"events": [
{
"eventid": "4419605",
"facebookeventid": "",
"eventname": "",
"description": "",
"eventurl": "",
"additionallistingtext": "",
"status": "SalesEnded",
"tags": "",
"spotifyuri": "",
"dates": {
"startdate": "20120529010000",
"enddate": "20121231235500",
"onsaledate": "20120309084000",
"announcedate": "20120309115333",
"timezone": "EDT"
},
"venue": {
"venueid": "210795",
"name": "Online",
"venueurl": "",
"city": "Online",
"state": "",
"postalcode": "00000",
"country": "US",
"address": "Online",
"twitterid": "",
"venueimages": {
"large": "",
"small": ""
}
},
"eventimages": [""],
"prices": {
"pricelow": "$195.00",
"pricehigh": "$195.00",
"pricedisplay": "$195.00"
},
"attractionList": [
{
"sequence": "0",
"billing": "1.00",
"genre": "Seminar/Lecture",
"links": "",
"media": ""
},
{
"sequence": "1",
"billing": "0.75",
"links": "",
"media": ""
},
{
"sequence": "2",
"billing": "0.75",
"links": "",
"media": ""
}
]
},
答案 0 :(得分:2)
如果我理解正确,您有两种选择 - 更改服务器发送日期的格式,或者在客户收到日期后格式化日期。
如何实现后者的一个例子:
var dateStr = this.dates.startdate // 20120629100000
var formattedDate = dateStr.substr( 6, 2) + '/'
+ dateStr.substr( 4, 2) + '/'
+ dateStr.substr( 0, 4) + ' '
+ dateStr.substr( 8, 2) + ':'
+ dateStr.substr(10, 2) + ':'
+ dateStr.substr(12, 2);
... '<div class="date">' + formattedDate + '</div>' ...
此特定示例将输出:29/06/2012 10:00:00 (DD/MM/YYYY HH:MM:SS)