How to format a JSON date?

so i need to format the json date from this format

"9/30/2010 12:00:00 AM", this is MM / DD / YYYY HH: MM: SS for formatting as follows: DD / MM / YYYY , so I do not need information about hours, minutes and seconds, and I need to replace months and days with json, I tried several different ways, but it always failed

I need to do this using jQuery

also I did not find an answer to create this type of date, all I found was the date as follows: / Date (1224043200000) /

for anyone to get an idea?

+4
source share
4 answers

you can create a Date object from a string as follows:

var myDate = new Date(dateString); 

then you can manipulate it anyway, one way to get the desired result:

 var output = myDate.getDate() + "\\" + (myDate.getMonth()+1) + "\\" + myDate.getFullYear(); 

You can find more in this elated.com Dates article.

+4
source

Unfortunately, your "from" dateformat is not implementation independent in JavaScript. And all other formats are implementation-dependent, which means that even if this format will be understood by most of the implementation, I / you cannot be sure, for example, how the order of DD and MM will be handled (I'm almost sure that this will depend on local regional settings). Therefore, I would recommend using a third-party parser (or your hand) to get the Date object from your input string. You can find one such parser here: http://www.mattkruse.com/javascript/date/

Since your question is not 100% clear to me, it is possible that you have a date in the format / Date (number) /, which assumes that you are calling the ASP.Net service from your jQuery code. In this case, during JSON parsing, you can convert it to a Date object:

 data = JSON.parse(data, function (key, value) { // parsing MS serialized DateTime strings if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') { return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10)); // maybe new Date(parseInt(value.substr(6))) also works and it simpler } return value; }); 
0
source

The code below solved my problem:

 var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10)); var day = date.getDate(); var monthIndex = date.getMonth(); var year = date.getFullYear(); 
-one
source

Try something like this:

 var date = new Date(parseInt(jsonDate.substr(6))); 

where jsonDate is a variable that stores your date

-2
source

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


All Articles