0

I'm trying to convert data using JS ES6 Intl.DateTimeFormat("pt-BR"), but everything i get is the previous day. Code i'm using:

var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
console.log(a.format(new Date("2015-01-02"))); // "1/1/2015"
console.log(b.format(new Date("2015-01-02"))); // "01/01/2015"

Thank you in advance.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Per your comment below, if all you want to do is reformat the string 2019-01-22 to 22/01/2019, then do that and don't mess with Date objects at all: `let [y, m, d] = '2019-01-22'.split(/\D/); let s = [m,d,y].join('/')`. PS the format for pt-BR is d/m/y (which is much more common than en-US m/d/y). – RobG Jan 04 '20 at 09:04
  • Is there any issue about performance or code integrity using Date objects, instead of your code above? – Douglas Vicentini Jan 06 '20 at 16:39
  • A solution using a Date would likely be imperceptibly slower. The string method is just simpler. – RobG Jan 07 '20 at 00:40

2 Answers2

0

The problem is related to timezone. The output is in your timezone, at 00:00 of the day 2nd of January UTC is 1st January 21:00 at your timezone, considering that your timezone is, for example, America/Sao_Paulo.

see: doc from mozilla

timeZone

The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".

0

It is discouraged to rely on Date parsing a string. According to mdn:

Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies.

The format you use (yyyy-mm-dd) is interpreted as a UTC date (at midnight), so it does not correspond with your local date.

So better break down the string yourself into numerical arguments, and pass those to the Date constructor:

var a = new Intl.DateTimeFormat("en-US");
var b = new Intl.DateTimeFormat("pt-BR");
let date = new Date(..."2015-01-02".match(/\d+/g).map((d, i) => d-i%2));
console.log(a.format(date)); // "1/1/2015"
console.log(b.format(date)); // "01/01/2015"
trincot
  • 317,000
  • 35
  • 244
  • 286
  • If regex is the issue, you can also do `.split("-")`. – trincot Jan 03 '20 at 19:33
  • I just added `var options = {timeZone: "UTC"}; ` and it worked. – Douglas Vicentini Jan 03 '20 at 19:37
  • Still remains that you dismiss the warning on mdn, which I quoted ;-) – trincot Jan 03 '20 at 19:47
  • I didn't dismiss. I took a look at it, but I couldn't reach the solution based on that link. My problem is that i get the date from database like this `2019-01-22` and i just need to convert it to `22/01/2019`. I don't want to use libs for this anymore. I'm sorry if I missed something. – Douglas Vicentini Jan 03 '20 at 20:00
  • You pass a string (from the database) to the `Date` constructor. This is bad practice. You continue to do it. To me that corresponds to dismissing a warning. I can understand you don't want to use libs, but I didn't suggest using libs. – trincot Jan 03 '20 at 20:05
  • I used `new Date()` as you told me and it worked the same way. I was trying to get the shortest code for this problem, but I realized sometimes we need a few extra lines to keep code integrity. – Douglas Vicentini Jan 03 '20 at 20:45