Daylight Saving Date Conversion

6

Currently on my system I use the following code, to convert the dates coming into ajax. new Date(parseInt(data.replace(/\/Date\((-?\d+)\)\//, '$1')))

Since the variable data is a string that comes in '/Date(1508551200000)/' format, so you can substitute the variable for that string to test.

Now my problem began to appear on this date, because when I convert it with the above code it gives the following result:

  

Fri Oct 20 2017 23:00:00 GMT-0300 (Brazil's official time)

But the actual value is 10/21/2017 , when doing a search it turns out that this date was the start of daylight saving time of 2017 and with that it decreased by 1hr, the dates in daylight saving time are having this same problem.

How can I do this conversion so that it ignores this and returns me the value I want, without decreasing by 1hr, in case ignoring this DST conversion.

    
asked by anonymous 21.03.2018 / 21:04

1 answer

1

Use the toLocaleString() method and then convert the result to Date object:

var data = '/Date(1508551200000)/';
data = new Date(parseInt(data.replace(/\/Date\((-?\d+)\)\//, '$1'))).toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' });
var dt = data.substring(0, 10).split("/");
data = new Date(dt[2], dt[1] - 1, dt[0]);

console.log(data);

JSFiddle = > Sat Oct 21 2017 00:00:00 GMT-0300 (Brazil's official time)

    
21.03.2018 / 21:19