sledgeweight
sledgeweight

Reputation: 8095

json date format with a specific syntax

I know there are several good quality answers to this question but i'm struggling with the syntax to get it to work with:

$.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>");

            });
        });

where

<div class=date>" +this.dates.startdate+ "</div> 

part of the .append is the bit I'm wanting to format. I've tried numerous methods but not sure how to incorporate the more universal json format methods into the syntax above.

EDIT: Date format i would ideally liek would be: Sat 17th Nov, although 17.11.12 would be ok too.

the output i current get is 20120629100000. There is no forward or backslashes (/Date(1224043200000)/) like I've seen on similar questions, I'm not sure if there is an element of good coding practise I'm missing here. My jSON file is completely valid entered below:

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

Upvotes: 0

Views: 170

Answers (1)

billyonecan
billyonecan

Reputation: 20250

If I understand correctly you have two options - change the format of the date sent by the server, or format the date once the client receives it.

An example of how to achieve the latter:

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>' ...

This particular example would output: 29/06/2012 10:00:00 (DD/MM/YYYY HH:MM:SS)

Upvotes: 2

Related Questions