Convert JSON dates without timezone

I have a web service that returns, for example, a DateTime: DepartureDate object. I use ajax to get it and, in my opinion, convert a JSON date string to a javascript date object using this function:

function convertToDate(jsonDate) {
    return eval("new " + jsonDate.substring(1, jsonDate.length - 1));
}

The problem is that it new Date()takes into account local time on the client computer, so customers in different countries receive different dates. I want to get the exact date that was returned from the webservice. Is there an easy way to do this?

+5
source share
1 answer

The problem is that it new Date()takes into account local time on the client computer.

. UTC.

, UTC + 1:

new Date(0)   // Thu Jan 01 1970 01:00:00 GMT+0100 (CET)

, toString 01:00:00, , . 01:00:00 UTC + 1 00:00:00 UTC, , 0.

, , UTC, date.toUTCString() getUTCFullYear(), getUTCMonth() ..

, , eval.

new Date(parseInt(jsonDate.slice(6, -1), 10))
+6

All Articles