0

Using GET I receive an array and one of the data items is a timestamp string in the format:

 "date":"2017-01-11 12:19:15",

I have a function that needs to show specific data from this GET request for the current day only.

I think I would need to run an each loop and select only items that have the date: "2017-05-28", ignoring the "12:19:15" part.

Is there an easy and clean way to do this?

Many thanks!

teep
  • 53
  • 1
  • 11

1 Answers1

1

Use Date.parse() (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) to parse the date, then iterate over the list of parsed dates, only selecting those within the range you seek.

When using Date.parse(), you might want to be familiar with this Why does Date.parse give incorrect results?

Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
  • var orderDate = (new Date(Date.parse(JSON.data[i].date))).toDateString(); So here I am trying to make a new date from the json date string, then convert it back to a date string, but this code won't work, is there a way to do this in one statement? – teep May 28 '17 at 17:31