te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to format numbers and dates based on user locale settings? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to format numbers and dates based on user locale settings? - Stack Overflow

programmeradmin5浏览0评论

I need a way to automatically format Date and Number objects based on locale settings of my users.

So far, I've been using toLocaleString() function for dates. For numbers, toLocaleString() is also available, but as you can see in the jsFiddle I've prepared, results vary greatly between browsers. With English (United States) locale on my Windows machine, I get this:

  • IE9: 15,000.00
  • Firefox: 15,000
  • Chrome: 15000

In Chrome, it seems like toLocaleString() does not work at all for numbers. Except this approach, I've also tried:

  • To use MicrosoftAjax.js library localeFormat() function, but no matter which locale I've set on my PC (by using "Region and Language" dialog), dates and numbers were still both formated in US format.
  • To use libraries like Globalize. Although they do offer formatting capabilities, they are not able to detect user's locale automatically.

So, to summarize: how to automatically format numbers and dates to respect regional settings of user that browses the webpage in a way that works in all major browsers (IE, Firefox, Chrome)?

I need a way to automatically format Date and Number objects based on locale settings of my users.

So far, I've been using toLocaleString() function for dates. For numbers, toLocaleString() is also available, but as you can see in the jsFiddle I've prepared, results vary greatly between browsers. With English (United States) locale on my Windows machine, I get this:

  • IE9: 15,000.00
  • Firefox: 15,000
  • Chrome: 15000

In Chrome, it seems like toLocaleString() does not work at all for numbers. Except this approach, I've also tried:

  • To use MicrosoftAjax.js library localeFormat() function, but no matter which locale I've set on my PC (by using "Region and Language" dialog), dates and numbers were still both formated in US format.
  • To use libraries like Globalize. Although they do offer formatting capabilities, they are not able to detect user's locale automatically.

So, to summarize: how to automatically format numbers and dates to respect regional settings of user that browses the webpage in a way that works in all major browsers (IE, Firefox, Chrome)?

Share Improve this question edited Jan 11, 2014 at 5:28 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Jun 26, 2012 at 10:59 Nikola AnusevNikola Anusev 7,0882 gold badges31 silver badges47 bronze badges 2
  • On server side you can detect locale from the Accept-Language header that browsers send. Using that your server can send javascript depending on the locale. Edit: it seems you can also get it from navigator.language, I don't know how supported that is though if some library wasn't capable of using it. – Esailija Commented Jun 26, 2012 at 11:04
  • Using Accept-Language is wrong and pletely useless. For example, my system is set up to use English (because a translated OS is an unusable OS), but I use either Norwegian or ISO date formats (because UK and US date formats are impractical, and I'm not based in the UK or US). I think this is not an edge case, but representative for a wide range of users across the world. – Florian Winter Commented Oct 29, 2019 at 9:44
Add a ment  | 

2 Answers 2

Reset to default 11

.toLocaleString() functions on native Javascript objects is practically useless because it does not let you specify the locale or otherwise control their behavior.

Until the ECMAScript i18n API bees a reality (which is probably too far in the future to be worth considering at all right now) your only practical option is using libraries such as Globalize, but then as you say you need to detect the user's preferred locale.

Detecting the locale is another problem that is not easily solved with pure Javascript. Specifically, the Accept-Language header is IMHO practically useless as a means of locale detection because it is not visible to the vast majority of web users. That's why web applications typically provide a custom mechanism for the user to select a locale which is municated back to the server, and the server uses this information to configure each response thereafter.

Today the Internationalization API @Jon mentioned is widely supported. From a primitive number you can use Number(123456).toLocaleString() and Chrome now returns "123,456" as expected (for a US locale). I haven't tested on IE11/Edge but support is there according to caniuse.

发布评论

评论列表(0)

  1. 暂无评论