{"id":8950,"date":"2019-09-13T14:25:48","date_gmt":"2019-09-13T06:25:48","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8950"},"modified":"2019-09-16T22:34:36","modified_gmt":"2019-09-16T14:34:36","slug":"js-intl-zh","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/09\/js-intl-zh\/","title":{"rendered":"JS Intl\u5bf9\u8c61\u5b8c\u6574\u7b80\u4ecb\u53ca\u5728\u4e2d\u6587\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8950\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8950<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<br \/>\n<script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/201909-intl.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/intl-cover2.jpg\" width=\"484\" height=\"300\" class=\"alignright clip thumb size-medium\" alt=\"JS Intl\u5bf9\u8c61\u5360\u4f4d\u56fe\" \/><\/p>\n<h3>\u4e00\u3001\u5148\u6574\u4f53\u4e86\u89e3Intl\u5bf9\u8c61<\/h3>\n<p>Intl\u5bf9\u8c61\u662fECMAScript\u56fd\u9645\u5316API\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u5b83\u63d0\u4f9b\u5bf9\u8bed\u8a00\u654f\u611f\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u3001\u652f\u6301\u6570\u5b57\u683c\u5f0f\u5316\u4ee5\u53ca\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u3002<\/p>\n<p>\u6211\u4eec\u5728\u63a7\u5236\u53f0\u8f93\u5165Intl\uff0c\u4f1a\u5f97\u5230\u5982\u4e0b\u56fe\u6240\u793a\u7684\u8fd4\u56de\u503c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-12_220529.png\" width=\"373\" height=\"247\" alt=\"Intl\u5bf9\u8c61\u8fd4\u56de\u503c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u4e0b\uff1a<\/p>\n<pre>{\r\n    Collator: \u0192 Collator()\r\n    DateTimeFormat: \u0192 DateTimeFormat()\r\n    ListFormat: \u0192 ListFormat()\r\n    NumberFormat: \u0192 NumberFormat()\r\n    PluralRules: \u0192 PluralRules()\r\n    RelativeTimeFormat: \u0192 RelativeTimeFormat()\r\n    getCanonicalLocales: \u0192 getCanonicalLocales()\r\n    v8BreakIterator: \u0192 v8BreakIterator()\r\n}<\/pre>\n<p>Intl\u5bf9\u8c61\u662f\u4eceIE11+\u5f00\u59cb\u652f\u6301\u7684\uff0c\u5176\u4e2d\u6709\u90e8\u5206\u5c5e\u6027\u65b9\u6cd5\u76ee\u524d\u4ec5Chrome\u6d4f\u89c8\u5668\u624d\u652f\u6301\uff0c\u4f8b\u5982ListFormat\u3002<\/p>\n<p>\u6211\u4eec\u4e00\u4e2a\u4e00\u4e2a\u4e86\u89e3\u4e0b\uff08Intl.v8BreakIterator\u6ca1\u6709\u76f8\u5173\u6587\u6863\uff0c\u6682\u65f6\u5ffd\u7565\uff09\u3002<\/p>\n<h3>\u4e8c\u3001Intl.Collator\u5bf9\u8c61<\/h3>\n<p>collator\u8fd9\u4e2a\u5355\u8bcd\u610f\u601d\u662f\u6392\u5e8f\u5668\u3002Intl.Collator\u5bf9\u8c61\u662f\u6392\u5e8f\u5668\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u652f\u6301\u5bf9\u8bed\u8a00\u654f\u611f\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre>new Intl.Collator([locales[, options]])<\/pre>\n<p>\u5176\u4e2d\u53c2\u6570<code>locales<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u503c\u662f<a href=\"https:\/\/tools.ietf.org\/html\/bcp47\" rel=\"noopener nofollow\" target=\"_blank\">BCF 47\u8bed\u8a00\u6807\u8bb0<\/a>\u7684\u5b57\u7b26\u4e32\uff08\u8bc4\u8bba\u6709\u53cd\u9988\u5e94\u8be5\u662fbcf47\u89c4\u8303\u4e2d\u7684tags\u6807\u7b7e\u6269\u5c55\uff0cunicode \u683c\u5f0f\uff09\uff0c\u6216\u6b64\u7c7b\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002<code>options<\/code>\u4e5f\u662f\u53ef\u9009\u53c2\u6570\uff0c\u652f\u6301\u5f88\u591a\u8f93\u5165\u503c\uff0c\u7531\u4e8e\u4e0d\u662f\u672c\u6587\u91cd\u70b9\uff0c\u4e0d\u5c55\u5f00\uff0c\u6709\u5174\u8da3\u53ef\u4ee5<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Collator\" rel=\"noopener nofollow\" target=\"_blank\">\u67e5\u770b\u8fd9\u91cc\u7684\u6587\u6863<\/a>\u3002<\/p>\n<p>Collator\u5b9e\u4f8b\u4ece\u5176\u539f\u578b\u7ee7\u627f\u4e86<code>compare()<\/code>\u548c<code>resolvedOptions()<\/code>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0c\u6211\u4eec\u4e3b\u8981\u5173\u5fc3<code>compare()<\/code>\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u5bf9\uff0c\u503c\u76f8\u7b49\u8fd4\u56de0\uff0c\u524d\u9762\u7684\u503c\u5927\u8fd4\u56de\u8d1f\u6570\uff0c\u524d\u9762\u7684\u503c\u5c0f\u8fd4\u56de\u6574\u6570\u3002<\/p>\n<p>\u6b64\u65b9\u6cd5\u53ef\u4ee5\u770b\u6210\u662f<code>String.prototype.localeCompare()<\/code>\u65b9\u6cd5\u66f4\u5168\u65b0\u7684\u7248\u672c\uff0c<code>locales<\/code>\u548c<code>options<\/code>\u53c2\u6570\u4e24\u8005\u4e5f\u90fd\u975e\u5e38\u7c7b\u4f3c\u3002<\/p>\n<p><code>compare()<\/code>\u65b9\u6cd5\u4e3b\u8981\u7528\u5728\u4e0d\u540c\u8bed\u8a00\u4e0b\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u4f8b\u5982\uff0c\u5728\u5fb7\u8bed\u4e2d\uff0c\u00e4\u548ca\u6392\u5e8f\u4e00\u6837\uff0c\u4f46\u662f\u5728\u745e\u5178\u8bed\u4e2d\uff0c\u00e4\u662f\u6392\u5728\u5b57\u6bcdz\u7684\u540e\u9762\u7684\uff0c\u56e0\u6b64\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u5728\u5fb7\u8bed\u4e2d\uff0c\u00e4\u548ca\u6392\u5e8f\u4e00\u6837<\/span>\r\nconsole.log(new Intl.Collator('de').compare('\u00e4', 'z'));\r\n<span style=\"color:green;\">\/\/ \u8fd4\u56de\u8d1f\u503c<\/span>\r\n\r\n<span style=\"color:green;\">\/\/ \u745e\u5178\u8bed\u4e2d\uff0c\u00e4\u662f\u6392\u5728\u5b57\u6bcdz\u7684\u540e\u9762<\/span>\r\nconsole.log(new Intl.Collator('sv').compare('\u00e4', 'z'));\r\n<span style=\"color:green;\">\/\/ \u8fd4\u56de\u6b63\u503c<\/span><\/pre>\n<p>\u4e0d\u8fc7\uff0c\u6211\u4eec\u90fd\u662f\u4e2d\u56fd\u7528\u6237\uff0c\u4e0a\u9762\u573a\u666f\u7528\u4e0d\u5230\uff0c\u90a3Intl.Collator\u5bf9\u8c61\u6709\u6ca1\u6709\u5b9e\u7528\u4ef7\u503c\u5462\uff1f\u662f\u6709\u7684\u3002<\/p>\n<p>options\u6709\u4e2a\u53ef\u9009\u53c2\u6570\u662f<code>numeric<\/code>\uff0c\u5e03\u5c14\u5c5e\u6027\uff0c\u662f\u5426\u6309\u7167\u6570\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u9ed8\u8ba4\u662f<code>false<\/code>\uff0c\u8fd8\u8bb0\u4e0d\u8bb0\u5f97\u4e0b\u9762\u7ecf\u5178\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u6392\u5e8f\u95ee\u9898\uff1a<\/p>\n<pre>['15', '2', '100'].sort();    \r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"100\", \"15\", \"2\"]<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-12_225256.png\" width=\"210\" height=\"40\" alt=\"\u5b57\u7b26\u4e32\u6392\u5e8f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u56e0\u4e3a\u6570\u5b57\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u7b26\u4e32\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u4f7f\u7528Intl.Collator\u5bf9\u8c61\u7684\u6570\u503c\u6392\u5e8f\u53c2\u6570\u8fdb\u884c\u6539\u9020\u4e0b\uff0c\u5219\u6700\u540e\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u7b26\u4e32\u5b57\u9762\u4e0a\u7684\u6570\u503c\u5927\u5c0f\u8fdb\u884c\u6392\u5e8f\u4e86\uff1a<\/p>\n<pre>['15', '2', '100'].sort(new Intl.Collator(undefined, { numeric: true }).compare);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"2\", \"15\", \"100\"]<\/span><\/pre>\n<p>\u4f8b\u5982\u63a7\u5236\u53f0\u8dd1\u51fa\u6765\u7684\u7ed3\u679c\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-12_225815.png\" width=\"528\" height=\"40\" alt=\"\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u9762\u503c\u6392\u5e8f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u540e\u6211\u4eec\u8bf4\u8bf4\u4e2d\u6587\u7684\u6392\u5e8f\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6211\u4eec\u7684\u4e2d\u6587\u6309\u7167\u9996\u5b57\u6bcd\u62fc\u97f3\u6392\u5e8f\uff0c\u8be5\u600e\u4e48\u5904\u7406\uff1f<\/p>\n<pre>var arrUsername = [\"\u9648\u5764\", \"\u9093\u8d85\", \"\u675c\u6df3\", \"\u51af\u7ecd\u5cf0\", \"\u97e9\u5e9a\", \"\u80e1\u6b4c\", \"\u9ec4\u6653\u660e\", \"\u8d3e\u4e43\u4eae\", \"\u674e\u6668\", \"\u674e\u6613\u5cf0\", \"\u9e7f\u6657\", \"\u4e95\u67cf\u7136\", \"\u5218\u70e8\", \"\u9646\u6bc5\", \"\u5b59\u7ea2\u96f7\"];<\/pre>\n<p>\u6211\u4eec\u5bf9\u4e0a\u9762arrUsername\u8fdb\u884c\u9ed8\u8ba4\u7684<code>sort()<\/code>\u6392\u5e8f\uff0c\u7ed3\u679c\u662f\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>arrUsername.sort();\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"\u4e95\u67cf\u7136\", \"\u51af\u7ecd\u5cf0\", \"\u5218\u70e8\", \"\u5b59\u7ea2\u96f7\", \"\u674e\u6613\u5cf0\", \"\u674e\u6668\", \"\u675c\u6df3\", \"\u80e1\u6b4c\", \"\u8d3e\u4e43\u4eae\", \"\u9093\u8d85\", \"\u9646\u6bc5\", \"\u9648\u5764\", \"\u97e9\u5e9a\", \"\u9e7f\u6657\", \"\u9ec4\u6653\u660e\"]<\/span><\/pre>\n<p>\u987a\u5e8f\u5c31\u4e0d\u5bf9\u4e86\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e2d\u6587\u7b80\u4f53\u7684BCF 47\u8bed\u8a00\u6807\u8bb0\u5b57\u7b26\u4e32<code>zh<\/code>\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>arrUsername.sort(new Intl.Collator('zh').compare);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"\u9648\u5764\", \"\u9093\u8d85\", \"\u675c\u6df3\", \"\u51af\u7ecd\u5cf0\", \"\u97e9\u5e9a\", \"\u80e1\u6b4c\", \"\u9ec4\u6653\u660e\", \"\u8d3e\u4e43\u4eae\", \"\u4e95\u67cf\u7136\", \"\u674e\u6668\", \"\u674e\u6613\u5cf0\", \"\u5218\u70e8\", \"\u9646\u6bc5\", \"\u9e7f\u6657\", \"\u5b59\u7ea2\u96f7\"]<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-12_233530.png\" width=\"401\" height=\"98\" alt=\"\u4e2d\u6587\u6309\u7167\u62fc\u97f3\u6392\u5e8f\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001Intl.DateTimeFormat\u5bf9\u8c61<\/h3>\n<p>\u5176\u5b9e\u53ef\u4ee5\u770b\u6210\u662f\u4f20\u7edf\u7684\u4e0b\u9762\u51e0\u4e2a\u65b9\u6cd5\u7684\u53d8\u8eab\uff1a<\/p>\n<p>Date.prototype.toLocaleString()<br \/>\nDate.prototype.toLocaleDateString()<br \/>\nDate.prototype.toLocaleTimeString()<\/p>\n<p><strong>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre>new Intl.DateTimeFormat([locales[, options]])<\/pre>\n<p><code>locales<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u548cIntl.Collator\u5bf9\u8c61\u4e00\u6837\u7684\u610f\u601d\uff0c\u6307\u56fd\u5bb6\u5730\u533a\u5bf9\u5e94\u7684BCP 47\u5b57\u7b26\u4e32\uff0c\u5177\u4f53\u7ec6\u8282\u975e\u5e38\u8d1f\u8d23\uff0c\u4e0d\u5c55\u5f00\uff0c\u4ec5\u4ec5\u4e2d\u6587<code>zh-*<\/code>\u540e\u9762\u7684\u7c7b\u578b\u4e00\u53cc\u624b\u90fd\u6570\u4e0d\u8fc7\u6765\u3002<code>options<\/code>\u4e5f\u662f\u53ef\u9009\u53c2\u6570\uff0c\u53ef\u4ee5\u6307\u5b9a\u65e5\u671f\u7c7b\u578b\uff0c\u65f6\u533a\uff0c\u4ee5\u53ca\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u7684\u5448\u73b0\u65b9\u5f0f\uff08\u5982\u662f\u5426\u88650\uff09\uff0c\u662f\u542624\u5c0f\u65f6\u5236\u663e\u793a\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u5c31\u662f\u6211\u4eec\u53ef\u4ee5\u5b9a\u5236\u6211\u4eec\u60f3\u8981\u5448\u73b0\u7684\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u5e0c\u671b\u51fa\u73b0\u7684\u65e5\u671f\u4fe1\u606f\u683c\u5f0f\u662f\uff1a\u201cxxxx\u5e74xx\u6708xx\u65e5 xx:xx:xx\u201d\u3002<\/p>\n<p>\u5219\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\uff1a<\/p>\n<pre>new Intl.DateTimeFormat('zh', {\r\n    year: 'numeric',  \r\n    month: '2-digit',\r\n    day: '2-digit',\r\n    hour: '2-digit',\r\n    minute: '2-digit',\r\n    second: '2-digit',\r\n    hour12: false\r\n}).format(new Date())<\/pre>\n<p>\u5176\u4e2d<code>'2-digit'<\/code>\u8868\u793a\u4e00\u5b9a\u4f7f\u75282\u4f4d\u6570\u5b57\u8868\u793a\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u6570\u503c\u4e0d\u8d85\u8fc710\uff0c\u4f1a\u81ea\u52a8\u5728\u524d\u9762\u52a00\u3002<code>hour12<\/code>\u8bbe\u7f6e\u4e3a<code>false<\/code>\u8868\u793a\u6211\u4eec\u91c7\u752824\u5c0f\u65f6\u5236\uff0c\u4e8e\u662f\u665a\u4e0a\u5c31\u4f1a\u662f21:00\u800c\u4e0d\u662f\u4e0b\u53489\u70b9\u3002<\/p>\n<p>\u6211\u4eec\u770b\u4e0bIE11\u6d4f\u89c8\u5668\u4e0b\u7684\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_000111.png\" width=\"289\" height=\"273\" alt=\"\u65f6\u95f4\u683c\u5f0f\u5316\u5728IE\u6d4f\u89c8\u5668\u4e0b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5b8c\u5168\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\uff0c\u4f46\u662f\u5728Chrome\u6d4f\u89c8\u5668\u548cFirefox\u6d4f\u89c8\u5668\u4e0b\uff0c\u5374\u4e0d\u662f\u4e2d\u6587\u7684\u5e74\u6708\u65e5\u800c\u662f\u659c\u6760\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_000458.png\" width=\"291\" height=\"226\" alt=\"Firefox\u6d4f\u89c8\u5668\u4e0b\u7684\u65f6\u95f4\u4e0d\u662f\u4e2d\u6587\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd8\u9700\u8981\u8fdb\u4e00\u6b65\u5b57\u7b26\u5904\u7406\u4e0b\uff0c\u6216\u8005\u6709\u5176\u4ed6\u8ba9Chrome\u3001Firefox\u6d4f\u89c8\u5668\u663e\u793a\u548cIE\u4e00\u6837\u7684\u5e74\u6708\u65e5\u4e2d\u6587\u7684\u65b9\u6cd5\uff0c\u6b22\u8fce\u8d50\u6559\u3002<\/p>\n<h3>\u56db\u3001Intl.ListFormat\u5bf9\u8c61<\/h3>\n<p>Intl.ListFormat\u53ef\u4ee5\u8ba9\u5bf9\u8bed\u8a00\u654f\u611f\u7684\u5217\u8868\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<p><strong>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre>new Intl.ListFormat([locales[, options]]) <\/pre>\n<p><code>locales<\/code>\u5c31\u662f<code>'zh-Hans'<\/code>\u6216\u8005<code>'en-US'<\/code>\u4e4b\u7c7b\u7684\u5b57\u7b26\uff0c<code>options<\/code>\u4e5f\u662f\u53ef\u9009\u53c2\u6570\uff0c\u652f\u6301\u4e0b\u9762\u8fd93\u4e2a\uff1a<\/p>\n<ul>\n<li><code>localeMatcher<\/code>\u6307\u5339\u914d\u7b97\u6cd5\u3002<\/li>\n<li><code>type<\/code>\u503c\u5185\u5bb9\u8fde\u63a5\u7c7b\u578b\u3002\u6709<code>'conjunction'<\/code>\u8868\u793aA\u3001B\u548cC\uff0c<code>'disjunction'<\/code>\u8868\u793aA\u3001B\u6216C\u3002<code>'unit'<\/code>\u8868\u793a\u503c\u8981\u5e26\u5355\u4f4d\u3002<\/li>\n<li><code>style<\/code>\u8868\u793a\u6837\u5f0f\uff0c\u652f\u6301\u503c\u6709<code>'long'<\/code>\uff0c<code>'short'<\/code>\u548c<code>'narrow'<\/code>\uff0c\u9ed8\u8ba4\u662f<code>'long'<\/code>\uff1b\u5982\u679c\u662f<code>'short'<\/code>\uff0c\u5219\u8868\u793aA\u3001B\u3001C\uff1b\u5982\u679c\u662f<code>'narrow'<\/code>\uff0c\u5219\u8868\u793aA B C\u3002<\/li>\n<\/ul>\n<p>\u770b\u4e00\u4e2a\u4f8b\u5b50\u5427\uff1a<\/p>\n<pre>var vehicles = ['Motorcycle', 'Bus', 'Car'];\r\n\r\nvar formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });\r\nconsole.log(formatter.format(vehicles));\r\n<span style=\"color:green;\">\/\/ \u8f93\u51fa\u7ed3\u679c\u662f\uff1a \"Motorcycle, Bus, and Car\"<\/span><\/pre>\n<p>\u5982\u679c\u8bbe\u7f6e\u8bed\u8a00\u662f\u4e2d\u6587\uff0c\u5219\u7ed3\u679c\u5219\u662f\u5355\u8bcd\u4f7f\u7528\u987f\u53f7\u5206\u9694\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var vehicles = ['Motorcycle', 'Bus', 'Car'];\r\n\r\nvar formatter = new Intl.ListFormat('zh', { style: 'long', type: 'conjunction' });\r\nconsole.log(formatter.format(vehicles));\r\n<span style=\"color:green;\">\/\/ \u8f93\u51fa\u7ed3\u679c\u662f\uff1a \"Motorcycle\u3001Bus\u548cCar\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_001728.png\" width=\"571\" height=\"123\" alt=\"\u8fde\u8bcd\u98ce\u683c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u5bf9\u8c61\u76ee\u524d\u8fd8\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5\uff0c\u4ec5Chrome72+\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u56e0\u6b64\uff0c\u5c31\u4e0d\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u3002<\/p>\n<h3>\u4e94\u3001Intl.NumberFormat\u5bf9\u8c61<\/h3>\n<p>Intl.NumberFormat\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u8bed\u8a00\u73af\u5883\u6700\u6570\u503c\u5b57\u7b26\u4e32\u8fdb\u884c\u4e0d\u540c\u7684\u5448\u73b0\u5904\u7406\u3002\u672c\u5bf9\u8c61\u53ef\u4ee5\u770b\u6210\u662f<code>Number.toLocaleString()<\/code>\u65b9\u6cd5\u7684\u5347\u7ea7\u7248\uff0c\u53c2\u6570\u548c\u542b\u4e49\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p><strong>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre>new Intl.NumberFormat([locales[, options]])<\/pre>\n<p><code>locales<\/code>\u5c31\u662f<code>'zh-Hans'<\/code>\u6216\u8005<code>'en-US'<\/code>\u8fd9\u4e9b<a href=\"https:\/\/tools.ietf.org\/html\/bcp47\" rel=\"noopener nofollow\" target=\"_blank\">BCP 47\u8bed\u8a00\u6807\u7b7e\u5b57\u7b26<\/a>\uff0c<code>options<\/code>\u4e5f\u662f\u53ef\u9009\u53c2\u6570\uff0c\u652f\u6301\u7684\u53c2\u6570\u6bd4\u8f83\u591a\uff0c\u7531\u4e8eIntl.NumberFormat\u6bd4\u8f83\u5e38\u7528\uff0c\u56e0\u6b64\u8fd9\u51e0\u4e2a\u53c2\u6570\u6211\u90fd\u7b80\u5355\u4ecb\u7ecd\u4e0b\u3002<\/p>\n<dl>\n<dt><strong>localeMatcher<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u533a\u57df\u8bbe\u7f6e\u5339\u914d\u7b97\u6cd5\u3002\u503c\u53ef\u4ee5\u662f<code>'lookup'<\/code>\u548c<code>'best fit'<\/code>\uff0c\u5176\u4e2d<code>'best fit'<\/code>\u662f\u9ed8\u8ba4\u503c\uff0c\u8fd9\u4e2a\u7b97\u6cd5\u53ef\u4ee5\u8ba9\u5339\u914d\u5668\u8fd0\u884c\u65f6\u63d0\u4f9b\u7684\u533a\u57df\u8bbe\u7f6e\u5c3d\u53ef\u80fd\u9002\u5408\u8bf7\u6c42\u800c\u4e0d\u662f\u57fa\u4e8e\u67e5\u627e\u7b97\u6cd5\u7684\u7ed3\u679c\uff0c\u4f8b\u5982\u8bbe\u7f6e<code>'zh'<\/code>\uff0c\u7b80\u4f53\u4e2d\u6587\u5730\u533a\u4f1a\u8ba4\u4e3a\u662f\u7b80\u4f53\u4e2d\u6587\uff0c\u7e41\u4f53\u4e2d\u6587\u5730\u533a\u4f1a\u8ba4\u4e3a\u662f\u7e41\u4f53\u4e2d\u6587\uff0c\u800c\u4e0d\u662f\u6307\u5b9a\u5339\u914d<sup>\u2460<\/sup>\u3002<code>'lookup'<\/code>\u7b97\u6cd5\u5219\u4e0e\u4e4b\u4e0d\u540c\uff0c\u9075\u5faaBCP 47\u4e2d\u6307\u5b9a\u7684\u67e5\u627e\u7b97\u6cd5\u3002\u6211\u4eec\u5e73\u65f6\u5f00\u53d1\u800c\u4e0d\u592a\u7528\u5230\u8fd9\u4e2a\u53c2\u6570\u3002<\/p>\n<p><span class=\"s\">\u2460 \u81ea\u5df1\u7684\u7406\u89e3\uff0c\u5982\u679c\u4e0d\u5bf9\uff0c\u6b22\u8fce\u7ea0\u9519<\/span><\/dd>\n<dt><strong>style<\/strong><\/dt>\n<dd>\u8868\u793a\u683c\u5f0f\u8f6c\u6362\u4f7f\u7528\u7684\u6837\u5f0f\u3002\u9ed8\u8ba4\u503c\u662f<code>'decimal'<\/code>\uff0c\u8868\u793a\u5c31\u662f\u6570\u5b57\uff0c\u503c\u8fd8\u53ef\u4ee5\u662f<code>'currency'<\/code>\uff0c\u8868\u793a\u4f7f\u7528\u8d27\u5e01\u683c\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u662f<code>'percent'<\/code>\uff0c\u8868\u793a\u767e\u5206\u6bd4\u683c\u5f0f\u3002<\/dd>\n<dt><strong>currency<\/strong><\/dt>\n<dd>\u4f7f\u7528\u8d27\u5e01\u683c\u5f0f\u65f6\u5019\u4f7f\u7528\u7684\u8d27\u5e01\u7c7b\u578b\u3002\u503c\u9700\u8981\u662f<a href=\"https:\/\/baike.baidu.com\/item\/ISO%204217\" rel=\"noopener nofollow\" target=\"_blank\">ISO 4217\u8d27\u5e01\u4ee3\u7801<\/a>\uff08\u4e09\u4e2a\u5b57\u6bcd\uff09\u3002\u5e38\u89c1\u7684\u6709\uff1a\u4eba\u6c11\u5e01 China Yuan Renminbi (CNY)\u3001\u6e2f\u5143 Hong Kong Dollar (HKD)\u3001\u65b0\u53f0\u5e01 New Taiwan Dollar (TWD)\u3001\u6b27\u5143Euro(EUR)\u3001\u7f8e\u5143 US Dollar (USD)\u3001\u82f1\u9551 Great British Pound(GBP)\u3001\u65e5\u5143 Japanese Yen (JPY)\u3002<\/p>\n<p>\u5982\u679c<code>style<\/code>\u53c2\u6570\u503c\u662f<code>'currency'<\/code>\uff0c\u5219\u8fd9\u91cc\u7684<code>currency<\/code>\u5c5e\u6027\u4e00\u5b9a\u8981\u63d0\u4f9b\u3002<\/dd>\n<dt><strong>currencyDisplay<\/strong><\/dt>\n<dd>\u5982\u4f55\u663e\u793a\u8bbe\u7f6e\u7684\u8d27\u5e01\u3002\u9ed8\u8ba4\u503c\u662f<code>'symbol'<\/code>\u8868\u793a\u7528\u56fe\u5f62\u663e\u793a\uff0c\u4f8b\u5982\u4eba\u6c11\u5e01\u662f<code>'\uffe5'<\/code>\uff0c\u5176\u4ed6\u53ef\u9009\u503c\u6709<code>'code'<\/code>\uff0c\u8868\u793a\u4f7f\u7528ISO\u8d27\u5e01\u4ee3\u7801\uff0c\u4eba\u6c11\u5e01\u5c31\u662f<code>'CNY'<\/code>\uff1b\u503c\u4e5f\u53ef\u4ee5\u662f<code>'name'<\/code>\uff0c\u4f7f\u7528\u5f53\u5730\u8be5\u8d27\u5e01\u7684\u540d\u79f0\uff0c\u4f8b\u5982\u7f8e\u5143\u662f<code>'dollar'<\/code>\uff0c\u4eba\u6c11\u5e01\u5219\u662f<code>'\u4eba\u6c11\u5e01'<\/code>\u3002<\/dd>\n<dt><strong>useGrouping<\/strong><\/dt>\n<dd>\u5e03\u5c14\u503c\u3002\u8868\u793a\u662f\u5426\u4f7f\u7528\u5206\u7ec4\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u5343\u4f4d\u5206\u9694\u7b26\u6216\u5343\/\u5341\u4e07\/\u5343\u4e07\u5206\u9694\u7b26\u3002\u9ed8\u8ba4\u503c\u662f<code>true<\/code>\u3002<\/dd>\n<dt><strong>minimumIntegerDigits<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u6700\u5c0f\u6574\u6570\u4f4d\u6570\u3002\u53ef\u80fd\u7684\u503c\u4e3a1\u523021\uff1b\u9ed8\u8ba4\u503c\u4e3a1\u3002<\/dd>\n<dt><strong>minimumFractionDigits<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u6700\u5c0f\u5c0f\u6570\u4f4d\u6570\u3002\u53ef\u80fd\u7684\u503c\u4e3a0\u523020\uff0c\u666e\u901a\u6570\u5b57\u548c\u767e\u5206\u6bd4\u683c\u5f0f\u7684\u9ed8\u8ba4\u503c\u4e3a0\uff0c\u8d27\u5e01\u683c\u5f0f\u7684\u9ed8\u8ba4\u503c\u662fISO 4217\u8d27\u5e01\u4ee3\u7801\u5217\u8868\u63d0\u4f9b\u7684\u6b21\u8981\u5355\u4f4d\u6570\u5b57\u7684\u6570\u91cf\uff08\u5982\u679c\u5217\u8868\u6ca1\u6709\u63d0\u4f9b\u8be5\u4fe1\u606f\u5219\u4f7f\u75282\u4ee3\u66ff\uff09\u3002<\/dd>\n<dt><strong>maximumFractionDigits<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u6700\u5927\u5c0f\u6570\u4f4d\u6570\u3002\u53ef\u80fd\u7684\u503c\u4e3a0\u523020\u3002\u666e\u901a\u6570\u5b57\u683c\u5f0f\u7684\u9ed8\u8ba4\u503c\u4e3aminimumFractionDigits\u548c3\u4e2d\u7684\u8f83\u5927\u8005\uff0c\u8d27\u5e01\u683c\u5f0f\u7684\u9ed8\u8ba4\u503c\u4e3aminimumFractionDigits\u548cISO 4217\u8d27\u5e01\u4ee3\u7801\u5217\u8868\u63d0\u4f9b\u7684\u5355\u4f4d\u6570\u5b57\u4e2d\u7684\u8f83\u5927\u8005\uff08\u5982\u679c\u5217\u8868\u672a\u63d0\u4f9b\u8be5\u4fe1\u606f\uff0c\u5219\u4e3a2\uff09\uff0c\u683c\u5f0f\u767e\u5206\u6bd4\u7684\u9ed8\u8ba4\u503c\u4e3aminimumFractionDigits\u548c0\u4e2d\u7684\u8f83\u5927\u8005\u3002<\/dd>\n<dt><strong>minimumSignificantDigits<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u6700\u5c0f\u6709\u6548\u4f4d\u6570\u3002\u53ef\u80fd\u7684\u503c\u4e3a1\u523021\uff1b\u9ed8\u8ba4\u503c\u4e3a1\u3002<\/dd>\n<dt><strong>maximumSignificantDigits<\/strong><\/dt>\n<dd>\u8981\u4f7f\u7528\u7684\u6700\u5927\u6709\u6548\u4f4d\u6570\u3002\u53ef\u80fd\u7684\u503c\u4e3a1\u523021\uff1b\u9ed8\u8ba4\u503c\u4e3a21\u3002<\/dd>\n<\/dl>\n<p>\u5b9e\u9645\u5f00\u53d1\uff0c\u6211\u4eec\u591a\u4f7f\u7528NumberFormat\u7ee7\u627f\u7684<code>format()\u65b9<\/code>\u6cd5\u5bf9\u6570\u5b57\u683c\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<h4>1. \u8fde\u7eed\u6570\u5b57\u5343\u4f4d\u5206\u9694\u7b26\u5206\u9694<\/h4>\n<p>\u7f51\u9875\u5f00\u53d1\uff0c\u5c24\u5176\u79fb\u52a8\u7aef\uff0c\u8fde\u7eed\u6574\u6570\u6570\u503c\u5efa\u8bae\u4f7f\u7528\u9017\u53f7\u5206\u9694\uff0c\u539f\u56e0\u89c1<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/09\/web-page-comma-number\/\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u65e0\u8bba\u662f\u4f7f\u7528<code>Number.toLocaleString()<\/code>\u65b9\u6cd5\uff0c\u8fd8\u662f\u8fd9\u91cc\u7684<code>new Intl.NumberFormat().format()<\/code>\u65b9\u6cd5\uff0c\u90fd\u53ea\u80fd\u67093\u4f4d\u5c0f\u6570\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>new Intl.NumberFormat().format(12345.6789);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"12,345.679\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_122111.png\" width=\"307\" height=\"64\" alt=\"\u6700\u591a3\u4f4d\u5c0f\u6570\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u662f4\uff0c\u5219\u53ef\u4ee5\u5982\u4e0b\u5904\u7406\uff1a<\/p>\n<pre>new Intl.NumberFormat(undefined, {\r\n    minimumFractionDigits: 4\r\n}).format(12345.6789);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"12,345.6789\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_130453.png\" width=\"265\" height=\"68\" alt=\"\u4fdd\u75594\u4e3a\u5c0f\u6570\u622a\u56fe\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679cminimumFractionDigits\u7684\u53c2\u6570\u503c\u8d85\u8fc7\u539f\u672c\u7684\u5c0f\u6570\u4e2a\u6570\uff0c\u5219\u6700\u540e\u4f1a\u88650\u3002\u88650\u8fd9\u79cd\u7279\u6027\u5bf9\u4e8e\u6574\u6570\u4e5f\u540c\u6837\u9002\u7528\u3002<\/p>\n<h4>2. \u6570\u5b57\u4e0d\u8db3\u4f4d\u6570\u88650<\/h4>\n<p>\u5b57\u7b26\u4e32\u8865\u5168ES6\u6709\u73b0\u6210\u7684API\uff0c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/07\/js-padstart-padend\/\">padStart()\u548cpadEnd()<\/a>\uff0c\u4e0d\u8fc7IE\u4e0d\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u9047\u5230\u9700\u8981\u6570\u5b57\u8865\u5168\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u8fd9\u91cc\u7684<code>format()<\/code>\u65b9\u6cd5\uff0cIE11+\u90fd\u652f\u6301\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5e0c\u671b\u5c0f\u4e8e10\u7684\u6570\u5b57\u524d\u9762\u90fd\u67090\uff0c\u53ef\u4ee5\u8fd9\u4e48\u5904\u7406\uff1a<\/p>\n<pre>new Intl.NumberFormat(undefined, {\r\n    minimumIntegerDigits: 2\r\n}).format(8);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"08\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_131745.png\" width=\"257\" height=\"64\" alt=\"\u6570\u5b57\u524d\u9762\u8865\u5168\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u4f4d\u6570\u8db3\u591f\u591a\uff0c\u8d85\u8fc73\u4f4d\uff0c\u53ef\u4ee5\u8bbe\u7f6e<code>useGrouping<\/code>\u4e3a<code>false<\/code>\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u9047\u5230\u9017\u53f7\u5206\u9694\u7684\u5c34\u5c2c\u4e86\u3002<\/p>\n<h4>3. \u91d1\u989d\u4e2d\u6587\u81ea\u5e26<\/h4>\n<p>\u7ed9\u4e00\u4e32\u6570\u5b57\uff0c\u540e\u9762\u81ea\u5e26\u4e2d\u6587\u201c\u5143\u201d\u3002\u53ef\u4ee5\u5982\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n<pre>new Intl.NumberFormat('zh-Hans', { \r\n    style: 'currency', \r\n    currency: 'CNY',\r\n    currencyDisplay: 'name'\r\n}).format(12345.6789)\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"12,345.68\u00a0\u4eba\u6c11\u5e01\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_132517.png\" width=\"251\" height=\"94\" alt=\"\u8d27\u5e01\u4eba\u6c11\u5e01\u663e\u793a\" class=\"alignnone size-medium\" \/><\/p>\n<h4>4. \u6570\u5b57\u53d8\u6210\u4e2d\u6587\u6570\u5b57\u663e\u793a<\/h4>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u8981\u663e\u793a\u661f\u671f\u51e0\uff0c\u4e0d\u9700\u8981\u518d\u5f04\u4e2a\u6570\u7ec4\u8fdb\u884c\u4e00\u4e00\u6620\u5c04\u4e86\uff0c\u8bd5\u8bd5\u4e0b\u9762\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>'\u661f\u671f' + new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(new Date().getDay());\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"\u661f\u671f\u4e94\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_132959.png\" width=\"343\" height=\"53\" alt=\"\u6570\u5b57\u53d8\u6210\u4e2d\u6587\u6c49\u5b57\u663e\u793a\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u516d\u3001Intl.PluralRules\u5bf9\u8c61<\/h3>\n<p>Intl.PluralRules\u5bf9\u8c61\u662f\u7528\u4e8e\u542f\u7528\u591a\u4e2a\u654f\u611f\u683c\u5f0f\u548c\u591a\u4e2a\u8bed\u8a00\u89c4\u5219\u7684\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p><strong>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre>new Intl.PluralRules([locales[, options]])<\/pre>\n<p>\u5176\u4e2d<code>locales<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u4e3aBCP 47\u8bed\u8a00\u6807\u7b7e\u5b57\u7b26\u4e32\u3002<code>options<\/code>\u4e5f\u662f\u53ef\u9009\u53c2\u6570\uff0c\u652f\u6301\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f<code>localeMatcher<\/code>\u8868\u793a\u67e5\u627e\u7b97\u6cd5\uff0c\u53e6\u5916\u4e00\u4e2a\u662f<code>type<\/code>\u8868\u793a\u7c7b\u578b\uff0c\u9ed8\u8ba4\u503c\u662f<code>'cardinal'<\/code>\uff0c\u8868\u793a\u57fa\u6570\uff08\u6307\u4e8b\u7269\u7684\u6570\u91cf\uff09\uff0c\u8fd8\u53ef\u4ee5\u662f\u503c<code>'ordinal'<\/code>\uff0c\u8868\u793a\u5e8f\u6570\uff08\u6307\u4e8b\u7269\u7684\u6392\u5e8f\u6216\u6392\u5e8f\uff0c\u4f8b\u5982\u82f1\u8bed\u4e2d\u7684\u201c1st\u201d\uff0c\u201c2nd\u201d\uff0c\u201c3rd\u201d\uff09\u3002<\/p>\n<p>\u6709\u4e2a<code>select()<\/code>\u65b9\u6cd5\u6bd4\u8f83\u5e38\u7528\uff0c\u6d4b\u8bd5\uff1a<\/p>\n<pre>new Intl.PluralRules('zh-Hans').select(0);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"other\"<\/span>\r\nnew Intl.PluralRules('zh-Hans').select(1); \r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"other\"<\/span>\r\nnew Intl.PluralRules('zh-Hans').select(2);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"other\"<\/span>\r\nnew Intl.PluralRules('zh-Hans').select(6);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"other\"<\/span>\r\nnew Intl.PluralRules('zh-Hans').select(18);\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"other\"<\/span><\/pre>\n<p>\u53ef\u89c1\u8fd9\u4e2a\u5bf9\u8c61\u5bf9\u4e8e\u4e2d\u6587\u7528\u6237\u800c\u8a00\uff0c\u6ca1\u4ec0\u4e48\u9524\u5b50\u7528\u3002<\/p>\n<h3>\u4e03\u3001Intl.RelativeTimeFormat\u5bf9\u8c61<\/h3>\n<p>\u6b64\u5bf9\u8c61IE\u4e0d\u652f\u6301\uff0cChrome71+\u5f00\u59cb\u652f\u6301\u3002\u8868\u793a\u76f8\u5bf9\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u3002\u4f8b\u5982\u6628\u5929\u660e\u5929\u8fd9\u79cd\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>new Intl.RelativeTimeFormat([locales[, options]])<\/pre>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u770b\u770b\u5728\u4e2d\u6587\u4e2d\u7684\u8868\u73b0\uff08\u82f1\u6587\u73af\u5883\u4e0b\u8868\u73b0\u53c2\u89c1<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/RelativeTimeFormat\" rel=\"noopener nofollow\" target=\"_blank\">MDN\u6587\u6863<\/a>\uff09\u3002<\/p>\n<pre>var rtf = new Intl.RelativeTimeFormat('zh');\r\n<span style=\"color:green;\">\/\/ -1\u8868\u793a\u524d\u4e00\u5929<\/span>\r\nrtf.format(-1, 'day');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"1\u5929\u524d\"<\/span>\r\n<span style=\"color:green;\">\/\/ 1\u8868\u793a\u5f80\u540e\u4e00\u5929<\/span>\r\nrtf.format(1, 'day');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"1\u5929\u540e\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_135443.png\" width=\"321\" height=\"131\" alt=\"\u4e00\u5929\u524d\u4e00\u5929\u540e\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u6211\u4eec\u8bbe\u7f6e\u5176\u4ed6\u7684\u53c2\u6570\uff0c\u5982\u4e0b\uff0c\u5219\u8fd4\u56de\u7ed3\u679c\u66f4\u6709\u610f\u601d\uff1a<\/p>\n<pre>var rtf = new Intl.RelativeTimeFormat('zh', {\r\n    numeric: 'auto'\r\n});\r\n\r\nrtf.format(-1, 'day');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"\u6628\u5929\"<\/span>\r\nrtf.format(1, 'day');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a\"\u660e\u5929\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_135705.png\" width=\"323\" height=\"149\" alt=\"\u6628\u5929\u548c\u660e\u5929\u663e\u793a\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u516b\u3001Intl.getCanonicalLocales()\u65b9\u6cd5<\/h3>\n<p>Intl.getCanonicalLocales()\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u89c4\u8303\u8bed\u8a00\u73af\u5883\u540d\u79f0\u7684\u6570\u7ec4\u3002\u91cd\u590d\u9879\u5c06\u88ab\u5ffd\u7565\uff0c\u5143\u7d20\u5c06\u88ab\u9a8c\u8bc1\u4e3a\u7ed3\u6784\u4e0a\u6709\u6548\u7684\u8bed\u8a00\u6807\u8bb0\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>Intl.getCanonicalLocales('zh-hans');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"zh-Hans\"]<\/span>\r\nIntl.getCanonicalLocales('zh');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"zh\"]<\/span>\r\nIntl.getCanonicalLocales('zh-cmn-Hans-CN');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"cmn-Hans-CN\"]<\/span>\r\nIntl.getCanonicalLocales('zh-cn');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"zh-CN\"]<\/span>\r\nIntl.getCanonicalLocales('yue-hk');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"yue-HK\"]<\/span>\r\nIntl.getCanonicalLocales('zh-some');\r\n<span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f\uff1a[\"zh-Some\"]<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/2019-09-13_140340.png\" width=\"315\" height=\"262\" alt=\"\u83b7\u53d6\u5b98\u65b9\u672c\u5730\u8bed\u8a00\u6807\u8bb0\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e5d\u3001\u4e2d\u79cb\u4f73\u8282\u7ed3\u675f\u8bed<\/h3>\n<p>\u79cb\u9ad8\u6c14\u723d\u9633\u5149\u660e\u5a9a\uff0c\u9886\u5bfc\u5e26\u4e24\u4e2a\u5c0f\u670b\u53cb\u53bb\u6e38\u4e50\u573a\u73a9\u4e86\uff0c\u6211\u4e00\u4e2a\u4eba\u5728\u5bb6\u5199\u4ee3\u7801\u64b8\u6280\u672f\u6587\u7ae0\uff0c\u597d\u4e0d\u5feb\u4e50\u3002<\/p>\n<p>\u73b0\u5728\u5e74\u9f84\u4e0a\u53bb\u4e86\uff0c\u5e73\u65f6\u7684\u7cbe\u529b\u5df2\u7ecf\u8ddf\u4e0d\u4e0a\u5e74\u8f7b\u4eba\u4e86\uff0c\u597d\u5728\u8fd8\u6709\u8282\u5047\u65e5\uff0c\u4f60\u53bb\u73a9\u800d\u6211\u5728\u5b66\u4e60\uff0c\u563f\u563f\u563f\u3002<\/p>\n<p>\u6628\u665a\u5728\u201c\u751f\u6d3b\u4e0e\u521b\u4f5c\u201d\u7248\u5757\u53d1\u4e86\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/life\/2019\/09\/moon-cake\/\">\u592a\u611f\u52a8\u4e86\uff0c\u4eca\u5e74\u7ec8\u4e8e\u5403\u5230\u516c\u53f8\u7684\u6708\u997c\u4e86\uff01<\/a>\u201d\u7684\u6587\u7ae0\uff0c\u95f2\u6765\u65e0\u4e8b\u7684\u670b\u53cb\u53ef\u4ee5\u53bb\u770b\u770b\u3002<\/p>\n<p>\u672c\u6587\u5185\u5bb9\u4e5f\u5f88\u591a\uff0c\u6709\u4e9b\u4e1c\u897f\u6211\u4e5f\u4e0d\u662f\u975e\u5e38\u4e86\u89e3\uff0c\u52a0\u4e0a\u884c\u6587\u4ed3\u4fc3\uff0c\u4e00\u5b9a\u4f1a\u5b58\u5728\u4e00\u4e9b\u6709\u9519\u8bef\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u5927\u5bb6\u5e2e\u5fd9\u6307\u6b63\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb\u4ee5\u53ca\u5e73\u65e5\u91cc\u7684\u652f\u6301\uff0c\u795d\u5927\u5bb6\u4e2d\u79cb\u8282\u6109\u5feb\uff0c\u73a9\u5f97\u597d\uff0c\u5403\u7684\u597d\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f913.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5185\u5bb9\u4f60\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u4ee5\u52a0\u5165\u6536\u85cf\u5939\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u800c\u4e14\u8fd8\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8950\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8950<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201909\/intl-cover2.jpg\" width=\"161\" height=\"100\" class=\"alignright size-medium\" alt=\"JS Intl\u5bf9\u8c61\" loading=\"lazy\" \/><\/p>\n<p>Intl\u5bf9\u8c61\u662fECMAScript\u56fd\u9645\u5316API\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u672c\u6587\u7b80\u5355\u4ecb\u7ecd\u76f8\u5173API\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4e2d\u6587\u73af\u5883\u4e0b\u7684\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u3001\u6570\u5b57\u683c\u5f0f\u5316\u4ee5\u53ca\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u3002\u5185\u5bb9\u8f83\u591a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[215,1487,1488,1493,1486,1273,1489,1495,1490,1491,1492,1496,1494],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8950"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=8950"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8950\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=8950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=8950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=8950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}