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
Probably a time zone problem.
– anonimo
@anonimo knows how to adjust the time zone?
– Shintaro Kisaragi
Want to put in
var agora
only the date or date and time?– Augusto Vasques
@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
– André Martins
That function will return the string you want.
– Sam