Remove Timezone on request with Node.js and express.js

Asked

Viewed 1,991 times

1

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

I’m making this requisition and consuming the json in a web application and in an app android nativo.

I would like to know how I could resolve it. Is there any possibility to remove the timezone in the API, or should I set something up in the database or server? I’m using MySQL.

Example:

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

**JSON:** "2017-10-02T13:58:34.000Z"
  • Can you put here the example of this string that comes from GET? has no time zone in the string?

  • I updated with the example.

  • Okay, two more questions: do you have control over the creation of the GET date? what is the BD date Timezone?

  • I do have. @@system_time_zone: BRT, @@global.time_zone: SYSTEM, @@session.time_zone: SYSTEM.

1 answer

2


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

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

For example having the date of JSON repairs in which 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'}));

  • So the only way is to treat field to field even? There is no way to remove or change Timezone directly in the application, right?

  • @Fernandozabin Yes, as you have no control over which country or Timezone the user is in, you have to manage it this way. Perhaps it is best to do the conversion when inserting.

  • I get it. I did a quick test on the web application and formatting the date with the standard Moment.js, it already fixes the time zone, had not noticed before. I think the solution to.

  • @Exact Fernandozabin could convert into ajax, so what reaches the server is normalized

Browser other questions tagged

You are not signed in. Login or sign up in order to post.