Convert date from json to regular format in jQuery mobile

I received from json with a date, the date format is "2011-03-13T11:30:00Z" , and I want to convert it to a normal format.

var Date= "Sunday, March 13th, 2011 " and var Time = "11:30"

I want to make it standalone as above with the appropriate format. please help me....

+4
source share
1 answer

Create a new Date object with a date string from json data, and then use the methods of the objects to get the desired date formats.

 var dateObject = new Date("2011-03-13T11:30:00Z"); var time = dateObject.getHours() + ':' + dateObject.getMinutes(); 

You also have the following that you could use to create your date

 dateObject.getDay(); // would return 0 for Sunday (days run 0-6 starting at Sun) dateObject.getMonth(); // would return 2 for March (months run 0-11) dateObject.getFullYear(); // return 2011 

According to the comments, in order to fix this for time zones, you need to know that the Z in your line means UTC/GMT , so if you are not in this time zone, you need to correct your difference in UTC

For example, replace Z with +05:30 5.5 hours earlier than UTC

 var dateString = "2011-03-13T11:30:00Z".replace('Z', '+05:30'); var dateObject = new Date(dateString); 
+3
source

Source: https://habr.com/ru/post/1483321/


All Articles