admin管理员组

文章数量:1315792

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.

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.

Share Improve this question asked Jan 24, 2017 at 13:29 Bruno QuaresmaBruno Quaresma 10.7k7 gold badges38 silver badges51 bronze badges 2
  • try new Intl.DateTimeFormat('pt-BR').format(date) Also, check this link – Tareq Commented Jan 24, 2017 at 13:35
  • toLocaleDateString is not supported in some of your environments. See this issue. – Robin Wieruch Commented Jan 20, 2021 at 8:16
Add a ment  | 

3 Answers 3

Reset to default 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));

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",

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

本文标签: javascripttoLocaleDateString is different using the same languageStack Overflow