0

我知道这个问题有几个高质量的答案,但我正在努力使用语法来让它工作:

$.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": ""
    }
  ]
},
4

1 回答 1

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)

于 2012-11-16T12:44:27.403 回答