First you need to understand something: dates have no format.
A date is just a concept, which represents the idea of a specific point in the timeline (or in a calendar system). In the case, 2019-12-12
and 12/12/2019
are just different ways of representing the same date (same values of day, month and year). As well as Dec 12th, 2019
or 12 de dezembro de 2019
, are different formats, but the date itself is the same.
Thus, the date in Mysql is not "in" a format. Only the date values are stored internally. When you query the data, then the date is shown in some format, but that doesn’t necessarily mean that it is written in that format.
So it’s not clear what you have. If 2019-12-12
for a string (i.e., a text representing the date), you can create a Date
and pass it on to Intl.DateTimeFormat
:
let data = new Date('2019-12-12');
console.log(new Intl.DateTimeFormat('pt-BR', {timeZone: 'UTC'}).format(data));
According to the documentation, when the string is in this format (year-month-day - or more specifically, in the format defined by ISO 8601 standard), the time is set to midnight and the date/time is considered to be in UTC.
However, the DateTimeFormat
by default considers the browser time zone, which may not be the same as UTC and give a difference in the time of formatting the date (for example, my browser uses the Time of Brasilia, and midnight of day 12 in UTC corresponds to 21h of the day 11 Time of Brasilia). That’s why I informed the timeZone: 'UTC'
in the builder.
In his reply worked because you also passed the time fields, and in this case Javascript considers the browser Timezone, so it is not necessary to inform that it is in UTC.
Apparently you’re passing the value as string to the
format
, but this method expects an objectDate
.– Woss
let data = 2019-12-12
- are you sure the code is like this? Because this is actually "2019 minus 12, minus 12" which results in the number 1995...– hkotsubo
is format that is returning from mysql
2019-12-14
– Ivana Momesso
But in this case it should be in quotes:
let data = '2019-12-12'
- without quotation marks, Javascript interprets as numbers– hkotsubo