0

I am displaying 50 concert details on a page, which come from an online JSON source.

The date is in JSON format and I want to convert and display it in the normal format.

I was going down the route of acquiring by class name, converting and then appending; but I have hit a bit of a wall.

eventTimeConv is empty as I was going to append my converted date into there:

<div class = "eventTimeConv"></div>
<div id = "event-time" class = "event-date-time">{{ show.datetime }}</div>

The javascript I have written up so far:

function changeDate()
{
  var date = document.getElementsByClassName("event-date-time");

  var newDate = new Date(date);
  var dateConverted = newDate.toDateString();

  var div = document.getElementsByClassName("eventTimeConv");
}

The conversion to dateConverted fails due to 'date' being a collection I'm assuming.

Can anyone provide some guidance here? Or an alternate method?

msanford
  • 11,803
  • 11
  • 66
  • 93
TS.
  • 49
  • 1
  • 9
  • 2
    there is no "JSON format" for dates... – n00dl3 Apr 19 '17 at 12:41
  • Sorry, wasn't sure of that, but thought that was what it was. What is the correct terminology? – TS. Apr 19 '17 at 12:45
  • @TyroneStock—just show the actual value, which seems to be `"datetime":"2017-04-19T17:00:00"`, which is ISO 8601 extended format. – RobG Apr 19 '17 at 20:45
  • This seems to be a duplicate of [*Why does Date.parse give incorrect results*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) and [*Where can I find documentation on formatting a date in JavaScript?*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Apr 19 '17 at 20:50

2 Answers2

1

if you like to try momentjs then just format your date as-

<div class = "eventTimeConv"></div>
<div id = "event-time" class = "event-date-time">
  {{ moment(show.datetime).format('YYYY-MM-DD h:mm') }}
</div>
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
1

GetElementsByClassName will return an array of all the divs with that class. So when you pass the date variable to new Date() function is not receiving a valid date string.

You will have to loop through that array and pick out the value you want to set to set the date for.

Jamie Clark
  • 252
  • 1
  • 13
  • *getElementsByClassName* returns a live [*HTMLCollection*](https://dom.spec.whatwg.org/#htmlcollection), which is Array-like, but it's not an Array. – RobG Apr 19 '17 at 20:53