最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript users incorrect locale with date formatting - Stack Overflow

programmeradmin6浏览0评论

In javascript I'm using Date.toLocaleDateString to format my dates in the user's locale. While in theory it should work, it doesn't.

I am located in the UK. My puter is set to UK and my default language is set to en/gb in both system settings and the browser content settings. Yet, Firefox always displays dates the US format. Is there some trick I'm missing?

The full code for formatting is this:

var timestamp = ...; //some value from ajax call
var dt = new Date(timestamp);
$('#audit-date').text(dt.toLocaleDateString());

In the UK for today's date I would expect to see 05/02/2014, but I see 02/05/2014, which is the US version of it.

In javascript I'm using Date.toLocaleDateString to format my dates in the user's locale. While in theory it should work, it doesn't.

I am located in the UK. My puter is set to UK and my default language is set to en/gb in both system settings and the browser content settings. Yet, Firefox always displays dates the US format. Is there some trick I'm missing?

The full code for formatting is this:

var timestamp = ...; //some value from ajax call
var dt = new Date(timestamp);
$('#audit-date').text(dt.toLocaleDateString());

In the UK for today's date I would expect to see 05/02/2014, but I see 02/05/2014, which is the US version of it.

Share Improve this question asked Feb 5, 2014 at 21:56 Aleks GAleks G 57.3k32 gold badges175 silver badges279 bronze badges 3
  • I use Chrome, my Windows machine region is set to UK/England/London, toLocaleDateString gives me US style dates. I think it's because we speak English so just download the default en-US version of the browser. I usually write dates in an international way which is unmistakable, for example 2014-02-05 for today. – Paul S. Commented Feb 5, 2014 at 22:00
  • What does (new Date()).toLocaleDateString() give? – NYRecursion Commented Feb 5, 2014 at 22:01
  • I'm also having this issue, On two machines in our office one returns toLocaleDateString the US way the other the UK way. I tried toLocaleDateString(window.navigator.language). But it just flips the problem as on the one returning the UK date it returns its language as "en-US" which makes no sense!! what is it based on? – znap026 Commented Sep 4, 2018 at 9:56
Add a ment  | 

2 Answers 2

Reset to default 6

Use this to pass the locale.

var locale = window.navigator.userLanguage || window.navigator.language;
alert(date.toLocaleString(locale));

A quick look into to awesome MDN Documentation tells me that you need a locale parameter, otherwise the result depends on the browser. https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

// British English uses day-month-year order
alert(date.toLocaleString("en-GB"));
// → "20/12/2012 03:00:00"

For more custom date formats I use the moment.js library. http://momentjs./

发布评论

评论列表(0)

  1. 暂无评论