1

I'm using this code.:

date.toLocaleDateString('pt-BR')
  • In my chrome browser the result is.: 9/13/2016
  • When I run the tests in local.: 2016-09-13
  • When I runt the tests in Circle CI.: 9/13/2016

What it happens?

Ps.: I'm using Jest and JSDom for my tests.

Bruno Quaresma
  • 9,457
  • 7
  • 32
  • 50
  • try `new Intl.DateTimeFormat('pt-BR').format(date)` Also, [check this link](http://stackoverflow.com/questions/21413757/tolocaledatestring-changes-in-ie11) – Tareq Jan 24 '17 at 13:35
  • toLocaleDateString is not supported in some of your environments. See this [issue](https://stackoverflow.com/questions/23199909/using-tolocalestring-in-node-js). – Robin Wieruch Jan 20 '21 at 08:16

3 Answers3

8

Use the toLocaleDateString options for set a fixed format.

var date = new Date();
var options = { year: 'numeric', month: '2-digit', day: '2-digit' };
console.log(date.toLocaleDateString('pt-BR', options));
Juan Picado
  • 1,823
  • 18
  • 33
2

You can add "LC_ALL="en_US.UTF-8" in the param test in package.json to fix it

"test": "LC_ALL=\"en_US.UTF-8\" npm run test",
ribas
  • 97
  • 1
  • 8
-2

Why do not use moment.js? It's simple and minimalist library to handle dates server side and client side, locales depend on the system and few things more, use moment and you will be sure the correct format moment.js

jesusgn90
  • 563
  • 2
  • 12