cancel
Showing results for 
Search instead for 
Did you mean: 

Json Date Conversion

former_member604901
Participant
2,943

Hi All,

I am getting a response of date in Json format /Date(253402300799000) which I want to show in as a normal date on my UI (dd/mm/yyyy). I have written a function to convert it but for certain scenarios it is failing like for date 9999-12-31 & 1900-01-01.

When I convert the above one it is giving me the date 1-1-10000 & 1-1-2040.

here is the function which I have Written :

for (var i = 0; i < oData.date.length; i++)

{

var oDate = oData.date[i].DATE.match(/\d+/g);

var oDate1 = new Date(parseInt(oDate));

var sDate = oDate1.getMonth() + 1 + "-" + oDate1.getDate() + "-" + oDate1.getFullYear();

oData.date[i].DATE = sDate;

}

Note : I am using xsoData.

Accepted Solutions (0)

Answers (3)

Answers (3)

0 Kudos

JSON does not know anything about dates. What .NET does is a non-standard hack/extension. The problem with dates in JSON and really JavaScript in general – is that there's no equivalent literal representation for dates. In JavaScript following Date constructor straight away converts the milliseconds since 1970 to Date as follows:

var jsonDate = new Date(1297246301973);

Then let's convert it to js format:

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

The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .

mvaibhav
Contributor
0 Kudos

Hi Sandeep,

The issue is not with your code but seems like there is some internal conversion happening from the back-end when the date is being returned.

The value which is being returned ( /Date(253402300799000)) results to date as Jan 01 10000.

Are there any chances that those 2 dates would be returned from your back-end ? If no, then probably you can ignore these.

Else you need to handle these explicitly when you date object results in these kind of dates.

Thanks,

vaibhav

smarchesini
Active Contributor
0 Kudos

Did you try this ?

/**
 * Converts milliseconds format to  
 * @param date milliseconds
 */
 function : setDateFromMillisecond(var s){
     var str = "/Date(253402300799000)";
     var res = str.substring(6, 21);
     var date = new Date(Number(res));
 }


Is millisecond the only type of input for data ?

former_member604901
Participant
0 Kudos

Hi Sebastiano,

I have tried it, but it is giving the wrong date as:-"Sat Jan 01 10000 05:29:59 GMT+0530 (India Standard Time)".

smarchesini
Active Contributor
0 Kudos

Sorry Sandeep,

what's time you needed ?