Date coming with date tomorrow instead of today

Asked

Viewed 342 times

3

My date’s coming up with the wrong date, tomorrow’s coming up plus I want today’s date. For my application the date and time cannot be wrong.
I’ve checked the date on the server and it’s correct:
date that should come: 2020-05-29T24:47:00
date that is coming: 2020-05-30T02:21:04
He’s a day and a few hours early And I’m not getting it fixed

Code(It runs on Nodejs on Server):

var agora = new Date().toISOString().replace(/\..+/, '')

thanks in advance for the help

  • Probably a time zone problem.

  • @anonimo knows how to adjust the time zone?

  • Want to put in var agora only the date or date and time?

  • @Shintarokisaragi you need to check the Timezone you want to apply, it was not very clear in your question, but you need to define this. if you are using sequelize just apply "Timezone": "Timezone you want", here is a list https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

  • That function will return the string you want.

1 answer

4


Update

The original answer was written initially using the method Date.prototype.toLocaleDateString() which could display date and time in the same call.
At the time the code was written it worked well, but with the evolution of the technology and constant adequacy of browsers the recommendations of WC3 today to old method functionality Date.prototype.toLocaleDateString() was transferred to the method Date.prototype.toLocaleString().
Currently the method Date.prototype.toLocaleDateString() only displays dates as the method Date.prototype.toLocaleTimeString() just displays the timetable.

Apparently your date is not coming wrong, you are having problems with the time zone. When you use the function toISOString() you are returning a string in format ISO 8601 the time zone of which is Meridian of Greenwich which is even that [UTC zero] 5

To fix the problem you must get this string using a method that returns a culturally located temporal version of that time and date. In javascript this method is toLocaleString().

The method toLocaleString() returns a string with the culturally located representation of the time and date.
The method accepts two parameters:

See the examples:

//Cria uma objeto Date contendo a hora e a data atual. 
let dataAtual = new Date();

//Data e hora no Meridiano de Greenwich(da forma que estava fazendo)
console.log(`Data e hora no Meridiano de Greenwich ${dataAtual.toISOString()}`);

//Data e hora na minha cidade
console.log(`Data Hora em Campo Grande ${dataAtual.toLocaleString("pt-Br",{
  dateStyle: "short",
  timeStyle: "short",
  timeZone: "America/Campo_Grande"
})}`);

//Data e hora em São Paulo
console.log(`Data Hora em São Paulo ${dataAtual.toLocaleString("pt-Br",{
  dateStyle: "short",
  timeStyle: "short",
  timeZone: "America/Sao_Paulo"
})}`);

//Somente a data em Manaus
console.log(`Data em Manus ${dataAtual.toLocaleString("pt-Br",{
  dateStyle: "short",
  timeZone: "America/Manaus"
})}`);

//Somente a hora em Fernando de Noronha
console.log(`Hora em Fernando de Noronha ${dataAtual.toLocaleString("pt-Br",{
  timeStyle: "short",
  timeZone: "America/Noronha"
})}`);

//Data e hora completa em Palmas
console.log(`Data Hora em Palmas ${dataAtual.toLocaleString("pt-Br",{
  dateStyle: "full",
  timeStyle: "full",
  timeZone: "America/Araguaina"
})}`);

Additional documentation: IANA time zone database: https://www.iana.org/time-zones

  • 1

    Very good, I didn’t know that option timeZone, will help immeasurably here or there. + 1 :)

  • 1

    @Luizfelipe has a look at this page https://en.wikipedia.org/wiki/Time_in_Brazil#Iana_time_zone_database has the list Brazilian time zones

Browser other questions tagged

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