Date coming with date tomorrow instead of today

My date is coming with the wrong date, ta coming tomorrow's date plus I want today's date. For my application the date and time cannot be wrong.
I checked the date on the server and this correct:
due date: 2020-05-29T24:47:00
date coming: 2020-05-30T02:21:04
He is 1 day and a few hours early and I am not getting ready

Code (it runs on NodeJS in the Server):

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

Since now I appreciate the help

Author: Shintaro Kisaragi, 2020-05-30

1 answers

Update

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

Apparently your date isn't coming wrong, you're having time zone issues. When you use the function toISOString() you are returning a string in format ISO 8601 whose time zone is Greenwich Meridian which is same as [UTC zero] 5

To fix the problem you should get this string using a method that returns a time and culturally localized version of that time it's data. In javascript this method is toLocaleString().

The toLocaleString() method returns a string with the culturally localized 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

 4
Author: Augusto Vasques, 2020-09-16 15:35:11