1

I get my JSON from my Own PHP file. I read the Json with Ajax and all is fine. But when I want to display the Date it shows me undefined.

My JSON TAG: "pubDate": "17.06.2015 15:59"

And my Ajax output: result[i].published

I tried something with moment.js but somehow I don't got the result I want.

How can I parse Json String to a Date in Ajax? Or is it in JSON a Data and I need the String and convert it in Ajax to data?

William-H-M
  • 1,050
  • 1
  • 13
  • 19
Solu
  • 73
  • 2
  • 9

1 Answers1

0

if you json string is like "pubDate": "17.06.2015 15:59" tha you code needs to be like result[i].pubDate because "published" is not exists in you json string.

for parsing date string to javascript date object you can try this library : http://www.datejs.com/ and this is sample code : http://www.datejs.com/2007/11/27/getting-started-with-datejs/

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • oh, sure you are right :D do i need to convert it in a date format? because i need to filter by date. And how i convert into date? – Solu Nov 28 '17 at 14:31
  • @Solu - for date format, you need to parse string in javascript and by that you can create date object in javascript --- but if your formate is working fine than you dont need to do any thing – Pranay Rana Nov 28 '17 at 14:33
  • @Solu - you can use this libarary http://www.datejs.com/ , for parsing date , check updated answer – Pranay Rana Nov 28 '17 at 14:41
  • OK Thanks for that hint. i did it with Date.parse and it works but not correctly. i have 2 input boxes with dates. 1st date is the start date and the 2nd date is the end date. iI parsed both Inputs and i also parsed result[i].pubDate, but i dont get all entries which are in the time period i selected ----- My Entries are form 04.12.2014 until 19.02.2015 and when i select startdate 03.12.2014 and enddate TODA, i have to get all entries, but i only get 2 – Solu Nov 28 '17 at 14:44
  • @Solu - have you included library file in you code as suggested ..http://www.datejs.com/2007/11/27/getting-started-with-datejs/ as suggest here – Pranay Rana Nov 28 '17 at 14:46
  • no i did not, but now i did, and i get the same result...only 2 entries – Solu Nov 28 '17 at 14:49
  • @Solu - you have to try this var d1 = Date.parse('07.15.2007'); – Pranay Rana Nov 28 '17 at 14:51
  • i did, like this : if( Date.parse(result[i].pubDate) > parsedDatum1 && Date.parse(result[i].pubDate) < parsedDatum2) – Solu Nov 28 '17 at 14:52
  • @Solu - first check this Date.parse(result[i].pubDate) giving you output or not – Pranay Rana Nov 28 '17 at 14:54
  • @Solu - and for comparison ob date you follow this answer of SO : https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Pranay Rana Nov 28 '17 at 14:56
  • @Solu - if its not working than you can wite logic of parsing string and create date object from that ...hint is you have to break string first by . and than by : and take value of splitted value and build date object – Pranay Rana Nov 28 '17 at 15:07
  • @Solu- is that worked for you .....I can provide code for prasing date but better you do that simple work of parsing by yourself , that way you will learn ...do upvote or accept answer if it worked for you – Pranay Rana Nov 28 '17 at 16:21