Remove timezone on request with node.js and express.js

1

I have API with node.js and express.js . In the database the fields of type datetime are saved correctly, but in the response of a request GET the date fields come with 3 more hours due to the time zone.

I am making this request and consuming json in a web application and in a android nativo app.

I wonder how I could solve it. Is there any possibility of removing timezone in the API, or should I configure something in the database or server? I'm using MySQL .

Example:

**DB:** 2017-10-02 10:58:34

**JSON:** "2017-10-02T13:58:34.000Z"
    
asked by anonymous 02.10.2017 / 16:53

1 answer

2

I suggest using a library for conversion. For example the extension of moment.js to timzones, moment-timzone .

To do it natively you can use UTC and add the difference to your schedule, or format the date with .toLocaleString('pt-br', {timezone: 'Brazil/brt'}) . The problems that may arise are in the days of switching between summer and winter time that I'm not sure the browser knows this information.

For example, having the JSON date looks at what we can extract:

var json = "2017-10-02T13:58:34.000Z";
var date = new Date(json);

console.log('Dia:', date.getUTCDate(), 'Hora:', [date.getUTCHours(), date.getUTCMonth() + 1, date.getUTCMinutes()].join(':'));

console.log(date.toLocaleString('pt-br', {timezone: 'Brazil/brt'}));
    
03.10.2017 / 08:50