使用javascript获取语言环境短日期格式


87

无论如何,我们是否可以使用JavaScript知道控制面板->区域和语言设置中使用的短日期格式?

我知道结合使用以下方法,我们可以获得Locale Long Name格式

toString()
toLocaleString()
toLocaleDateString()
toLocaleTimeString()

但是JavaScript中没有像这样的直接函数toLocaleShortDateString()

有没有可用的脚本来找出用户设置是什么?

谢谢。


只是为了确保我能理解:您是说很热衷于获取格式本身?因为您提供的“缺失函数”名称具有误导性(toLocaleShortDateString),可能类似于“ getLocaleShortDateFormat”?
Amitay Dobo 2010年

我发现此相关问题的答案对我的目的更有用,因为我只想提供一个不错的默认值,对于该默认值,此处提供的仅客户端解决方案很有用。
柯克沃尔2014年

Answers:


171
function getLocaleDateString(){

var formats = {
   "ar-SA" : "dd/MM/yy",
   "bg-BG" : "dd.M.yyyy",
   "ca-ES" : "dd/MM/yyyy",
   "zh-TW" : "yyyy/M/d",
   "cs-CZ" : "d.M.yyyy",
   "da-DK" : "dd-MM-yyyy",
   "de-DE" : "dd.MM.yyyy",
   "el-GR" : "d/M/yyyy",
   "en-US" : "M/d/yyyy",
   "fi-FI" : "d.M.yyyy",
   "fr-FR" : "dd/MM/yyyy",
   "he-IL" : "dd/MM/yyyy",
   "hu-HU" : "yyyy. MM. dd.",
   "is-IS" : "d.M.yyyy",
   "it-IT" : "dd/MM/yyyy",
   "ja-JP" : "yyyy/MM/dd",
   "ko-KR" : "yyyy-MM-dd",
   "nl-NL" : "d-M-yyyy",
   "nb-NO" : "dd.MM.yyyy",
   "pl-PL" : "yyyy-MM-dd",
   "pt-BR" : "d/M/yyyy",
   "ro-RO" : "dd.MM.yyyy",
   "ru-RU" : "dd.MM.yyyy",
   "hr-HR" : "d.M.yyyy",
   "sk-SK" : "d. M. yyyy",
   "sq-AL" : "yyyy-MM-dd",
   "sv-SE" : "yyyy-MM-dd",
   "th-TH" : "d/M/yyyy",
   "tr-TR" : "dd.MM.yyyy",
   "ur-PK" : "dd/MM/yyyy",
   "id-ID" : "dd/MM/yyyy",
   "uk-UA" : "dd.MM.yyyy",
   "be-BY" : "dd.MM.yyyy",
   "sl-SI" : "d.M.yyyy",
   "et-EE" : "d.MM.yyyy",
   "lv-LV" : "yyyy.MM.dd.",
   "lt-LT" : "yyyy.MM.dd",
   "fa-IR" : "MM/dd/yyyy",
   "vi-VN" : "dd/MM/yyyy",
   "hy-AM" : "dd.MM.yyyy",
   "az-Latn-AZ" : "dd.MM.yyyy",
   "eu-ES" : "yyyy/MM/dd",
   "mk-MK" : "dd.MM.yyyy",
   "af-ZA" : "yyyy/MM/dd",
   "ka-GE" : "dd.MM.yyyy",
   "fo-FO" : "dd-MM-yyyy",
   "hi-IN" : "dd-MM-yyyy",
   "ms-MY" : "dd/MM/yyyy",
   "kk-KZ" : "dd.MM.yyyy",
   "ky-KG" : "dd.MM.yy",
   "sw-KE" : "M/d/yyyy",
   "uz-Latn-UZ" : "dd/MM yyyy",
   "tt-RU" : "dd.MM.yyyy",
   "pa-IN" : "dd-MM-yy",
   "gu-IN" : "dd-MM-yy",
   "ta-IN" : "dd-MM-yyyy",
   "te-IN" : "dd-MM-yy",
   "kn-IN" : "dd-MM-yy",
   "mr-IN" : "dd-MM-yyyy",
   "sa-IN" : "dd-MM-yyyy",
   "mn-MN" : "yy.MM.dd",
   "gl-ES" : "dd/MM/yy",
   "kok-IN" : "dd-MM-yyyy",
   "syr-SY" : "dd/MM/yyyy",
   "dv-MV" : "dd/MM/yy",
   "ar-IQ" : "dd/MM/yyyy",
   "zh-CN" : "yyyy/M/d",
   "de-CH" : "dd.MM.yyyy",
   "en-GB" : "dd/MM/yyyy",
   "es-MX" : "dd/MM/yyyy",
   "fr-BE" : "d/MM/yyyy",
   "it-CH" : "dd.MM.yyyy",
   "nl-BE" : "d/MM/yyyy",
   "nn-NO" : "dd.MM.yyyy",
   "pt-PT" : "dd-MM-yyyy",
   "sr-Latn-CS" : "d.M.yyyy",
   "sv-FI" : "d.M.yyyy",
   "az-Cyrl-AZ" : "dd.MM.yyyy",
   "ms-BN" : "dd/MM/yyyy",
   "uz-Cyrl-UZ" : "dd.MM.yyyy",
   "ar-EG" : "dd/MM/yyyy",
   "zh-HK" : "d/M/yyyy",
   "de-AT" : "dd.MM.yyyy",
   "en-AU" : "d/MM/yyyy",
   "es-ES" : "dd/MM/yyyy",
   "fr-CA" : "yyyy-MM-dd",
   "sr-Cyrl-CS" : "d.M.yyyy",
   "ar-LY" : "dd/MM/yyyy",
   "zh-SG" : "d/M/yyyy",
   "de-LU" : "dd.MM.yyyy",
   "en-CA" : "dd/MM/yyyy",
   "es-GT" : "dd/MM/yyyy",
   "fr-CH" : "dd.MM.yyyy",
   "ar-DZ" : "dd-MM-yyyy",
   "zh-MO" : "d/M/yyyy",
   "de-LI" : "dd.MM.yyyy",
   "en-NZ" : "d/MM/yyyy",
   "es-CR" : "dd/MM/yyyy",
   "fr-LU" : "dd/MM/yyyy",
   "ar-MA" : "dd-MM-yyyy",
   "en-IE" : "dd/MM/yyyy",
   "es-PA" : "MM/dd/yyyy",
   "fr-MC" : "dd/MM/yyyy",
   "ar-TN" : "dd-MM-yyyy",
   "en-ZA" : "yyyy/MM/dd",
   "es-DO" : "dd/MM/yyyy",
   "ar-OM" : "dd/MM/yyyy",
   "en-JM" : "dd/MM/yyyy",
   "es-VE" : "dd/MM/yyyy",
   "ar-YE" : "dd/MM/yyyy",
   "en-029" : "MM/dd/yyyy",
   "es-CO" : "dd/MM/yyyy",
   "ar-SY" : "dd/MM/yyyy",
   "en-BZ" : "dd/MM/yyyy",
   "es-PE" : "dd/MM/yyyy",
   "ar-JO" : "dd/MM/yyyy",
   "en-TT" : "dd/MM/yyyy",
   "es-AR" : "dd/MM/yyyy",
   "ar-LB" : "dd/MM/yyyy",
   "en-ZW" : "M/d/yyyy",
   "es-EC" : "dd/MM/yyyy",
   "ar-KW" : "dd/MM/yyyy",
   "en-PH" : "M/d/yyyy",
   "es-CL" : "dd-MM-yyyy",
   "ar-AE" : "dd/MM/yyyy",
   "es-UY" : "dd/MM/yyyy",
   "ar-BH" : "dd/MM/yyyy",
   "es-PY" : "dd/MM/yyyy",
   "ar-QA" : "dd/MM/yyyy",
   "es-BO" : "dd/MM/yyyy",
   "es-SV" : "dd/MM/yyyy",
   "es-HN" : "dd/MM/yyyy",
   "es-NI" : "dd/MM/yyyy",
   "es-PR" : "dd/MM/yyyy",
   "am-ET" : "d/M/yyyy",
   "tzm-Latn-DZ" : "dd-MM-yyyy",
   "iu-Latn-CA" : "d/MM/yyyy",
   "sma-NO" : "dd.MM.yyyy",
   "mn-Mong-CN" : "yyyy/M/d",
   "gd-GB" : "dd/MM/yyyy",
   "en-MY" : "d/M/yyyy",
   "prs-AF" : "dd/MM/yy",
   "bn-BD" : "dd-MM-yy",
   "wo-SN" : "dd/MM/yyyy",
   "rw-RW" : "M/d/yyyy",
   "qut-GT" : "dd/MM/yyyy",
   "sah-RU" : "MM.dd.yyyy",
   "gsw-FR" : "dd/MM/yyyy",
   "co-FR" : "dd/MM/yyyy",
   "oc-FR" : "dd/MM/yyyy",
   "mi-NZ" : "dd/MM/yyyy",
   "ga-IE" : "dd/MM/yyyy",
   "se-SE" : "yyyy-MM-dd",
   "br-FR" : "dd/MM/yyyy",
   "smn-FI" : "d.M.yyyy",
   "moh-CA" : "M/d/yyyy",
   "arn-CL" : "dd-MM-yyyy",
   "ii-CN" : "yyyy/M/d",
   "dsb-DE" : "d. M. yyyy",
   "ig-NG" : "d/M/yyyy",
   "kl-GL" : "dd-MM-yyyy",
   "lb-LU" : "dd/MM/yyyy",
   "ba-RU" : "dd.MM.yy",
   "nso-ZA" : "yyyy/MM/dd",
   "quz-BO" : "dd/MM/yyyy",
   "yo-NG" : "d/M/yyyy",
   "ha-Latn-NG" : "d/M/yyyy",
   "fil-PH" : "M/d/yyyy",
   "ps-AF" : "dd/MM/yy",
   "fy-NL" : "d-M-yyyy",
   "ne-NP" : "M/d/yyyy",
   "se-NO" : "dd.MM.yyyy",
   "iu-Cans-CA" : "d/M/yyyy",
   "sr-Latn-RS" : "d.M.yyyy",
   "si-LK" : "yyyy-MM-dd",
   "sr-Cyrl-RS" : "d.M.yyyy",
   "lo-LA" : "dd/MM/yyyy",
   "km-KH" : "yyyy-MM-dd",
   "cy-GB" : "dd/MM/yyyy",
   "bo-CN" : "yyyy/M/d",
   "sms-FI" : "d.M.yyyy",
   "as-IN" : "dd-MM-yyyy",
   "ml-IN" : "dd-MM-yy",
   "en-IN" : "dd-MM-yyyy",
   "or-IN" : "dd-MM-yy",
   "bn-IN" : "dd-MM-yy",
   "tk-TM" : "dd.MM.yy",
   "bs-Latn-BA" : "d.M.yyyy",
   "mt-MT" : "dd/MM/yyyy",
   "sr-Cyrl-ME" : "d.M.yyyy",
   "se-FI" : "d.M.yyyy",
   "zu-ZA" : "yyyy/MM/dd",
   "xh-ZA" : "yyyy/MM/dd",
   "tn-ZA" : "yyyy/MM/dd",
   "hsb-DE" : "d. M. yyyy",
   "bs-Cyrl-BA" : "d.M.yyyy",
   "tg-Cyrl-TJ" : "dd.MM.yy",
   "sr-Latn-BA" : "d.M.yyyy",
   "smj-NO" : "dd.MM.yyyy",
   "rm-CH" : "dd/MM/yyyy",
   "smj-SE" : "yyyy-MM-dd",
   "quz-EC" : "dd/MM/yyyy",
   "quz-PE" : "dd/MM/yyyy",
   "hr-BA" : "d.M.yyyy.",
   "sr-Latn-ME" : "d.M.yyyy",
   "sma-SE" : "yyyy-MM-dd",
   "en-SG" : "d/M/yyyy",
   "ug-CN" : "yyyy-M-d",
   "sr-Cyrl-BA" : "d.M.yyyy",
   "es-US" : "M/d/yyyy"
};

   return formats[navigator.language] || 'dd/MM/yyyy';

} 

51
为什么这个列表很难在互联网上找到!
sidonaldson 2014年

6
这需要区分大小写,我的Safari会返回en-us。
QuentinUK 2015年

2
本地格式可以不同(自定义),取决于操作系统
iegik

2
@Chexpir Unicode CLDR项目是此列表的最终来源,而github.com/unicode-cldr/cldr-dates-modern是其消耗性稍强的版本。与以上数据相差甚远。:)
Paul Irish

4
@PaulIrish(new Intl.DateTimeFormat()).resolved.pattern似乎已消失。我得到了Uncaught TypeError: Cannot read property 'pattern' of undefined,我看到了一些铬去除它的bug。.resolvedOptions()可以,但是不包括模式。
webXL

66

我认为这是最好的方法...

    var date = new Date();

    var options = {
        weekday: "short",
        year: "numeric",
        month: "2-digit",
        day: "numeric"
    };

console.log(
    date.toLocaleDateString("en", options) //en is language option, you may specify..
);

更多选项及其示例。

 Option         Values and sample output

 weekday        narrow M
                short Mon
                long Monday

 year           2-digit 01
                numeric 2001

 month          2-digit 01
                numeric 1
                narrow J
                short Jan
                long January

day             2-digit 01
                numeric 1

hour            2-digit 12 AM
                numeric 12 AM

minute          2-digit 0
                numeric 0

second          2-digit 0
                numeric 0

timeZoneName    short 1/1/2001 GMT+00:00
                long 1/1/2001 GMT+00:00

请注意,IE(自10开始)不遵守这些选项,并且将返回您指定的月份名称。
Wholkaholism,

面对FF最新版本(25)表现出相同的行为;似乎只有Chrome支持这些选项。
Whelkaholism,

Safari也忽略选项。
wilsotc

1
任何人都达不到这个现在支持要好得多..铬和Firefox都已经支持一段时间的选项,IE11起支持.. Safari浏览器/ WebKit的还是不
TygerKrash

这是很棒的信息。如果您链接到选项的来源,那就更好了。
flipdoubt

13
function getLocaleShortDateString(d)
{
    var f={"ar-SA":"dd/MM/yy","bg-BG":"dd.M.yyyy","ca-ES":"dd/MM/yyyy","zh-TW":"yyyy/M/d","cs-CZ":"d.M.yyyy","da-DK":"dd-MM-yyyy","de-DE":"dd.MM.yyyy","el-GR":"d/M/yyyy","en-US":"M/d/yyyy","fi-FI":"d.M.yyyy","fr-FR":"dd/MM/yyyy","he-IL":"dd/MM/yyyy","hu-HU":"yyyy. MM. dd.","is-IS":"d.M.yyyy","it-IT":"dd/MM/yyyy","ja-JP":"yyyy/MM/dd","ko-KR":"yyyy-MM-dd","nl-NL":"d-M-yyyy","nb-NO":"dd.MM.yyyy","pl-PL":"yyyy-MM-dd","pt-BR":"d/M/yyyy","ro-RO":"dd.MM.yyyy","ru-RU":"dd.MM.yyyy","hr-HR":"d.M.yyyy","sk-SK":"d. M. yyyy","sq-AL":"yyyy-MM-dd","sv-SE":"yyyy-MM-dd","th-TH":"d/M/yyyy","tr-TR":"dd.MM.yyyy","ur-PK":"dd/MM/yyyy","id-ID":"dd/MM/yyyy","uk-UA":"dd.MM.yyyy","be-BY":"dd.MM.yyyy","sl-SI":"d.M.yyyy","et-EE":"d.MM.yyyy","lv-LV":"yyyy.MM.dd.","lt-LT":"yyyy.MM.dd","fa-IR":"MM/dd/yyyy","vi-VN":"dd/MM/yyyy","hy-AM":"dd.MM.yyyy","az-Latn-AZ":"dd.MM.yyyy","eu-ES":"yyyy/MM/dd","mk-MK":"dd.MM.yyyy","af-ZA":"yyyy/MM/dd","ka-GE":"dd.MM.yyyy","fo-FO":"dd-MM-yyyy","hi-IN":"dd-MM-yyyy","ms-MY":"dd/MM/yyyy","kk-KZ":"dd.MM.yyyy","ky-KG":"dd.MM.yy","sw-KE":"M/d/yyyy","uz-Latn-UZ":"dd/MM yyyy","tt-RU":"dd.MM.yyyy","pa-IN":"dd-MM-yy","gu-IN":"dd-MM-yy","ta-IN":"dd-MM-yyyy","te-IN":"dd-MM-yy","kn-IN":"dd-MM-yy","mr-IN":"dd-MM-yyyy","sa-IN":"dd-MM-yyyy","mn-MN":"yy.MM.dd","gl-ES":"dd/MM/yy","kok-IN":"dd-MM-yyyy","syr-SY":"dd/MM/yyyy","dv-MV":"dd/MM/yy","ar-IQ":"dd/MM/yyyy","zh-CN":"yyyy/M/d","de-CH":"dd.MM.yyyy","en-GB":"dd/MM/yyyy","es-MX":"dd/MM/yyyy","fr-BE":"d/MM/yyyy","it-CH":"dd.MM.yyyy","nl-BE":"d/MM/yyyy","nn-NO":"dd.MM.yyyy","pt-PT":"dd-MM-yyyy","sr-Latn-CS":"d.M.yyyy","sv-FI":"d.M.yyyy","az-Cyrl-AZ":"dd.MM.yyyy","ms-BN":"dd/MM/yyyy","uz-Cyrl-UZ":"dd.MM.yyyy","ar-EG":"dd/MM/yyyy","zh-HK":"d/M/yyyy","de-AT":"dd.MM.yyyy","en-AU":"d/MM/yyyy","es-ES":"dd/MM/yyyy","fr-CA":"yyyy-MM-dd","sr-Cyrl-CS":"d.M.yyyy","ar-LY":"dd/MM/yyyy","zh-SG":"d/M/yyyy","de-LU":"dd.MM.yyyy","en-CA":"dd/MM/yyyy","es-GT":"dd/MM/yyyy","fr-CH":"dd.MM.yyyy","ar-DZ":"dd-MM-yyyy","zh-MO":"d/M/yyyy","de-LI":"dd.MM.yyyy","en-NZ":"d/MM/yyyy","es-CR":"dd/MM/yyyy","fr-LU":"dd/MM/yyyy","ar-MA":"dd-MM-yyyy","en-IE":"dd/MM/yyyy","es-PA":"MM/dd/yyyy","fr-MC":"dd/MM/yyyy","ar-TN":"dd-MM-yyyy","en-ZA":"yyyy/MM/dd","es-DO":"dd/MM/yyyy","ar-OM":"dd/MM/yyyy","en-JM":"dd/MM/yyyy","es-VE":"dd/MM/yyyy","ar-YE":"dd/MM/yyyy","en-029":"MM/dd/yyyy","es-CO":"dd/MM/yyyy","ar-SY":"dd/MM/yyyy","en-BZ":"dd/MM/yyyy","es-PE":"dd/MM/yyyy","ar-JO":"dd/MM/yyyy","en-TT":"dd/MM/yyyy","es-AR":"dd/MM/yyyy","ar-LB":"dd/MM/yyyy","en-ZW":"M/d/yyyy","es-EC":"dd/MM/yyyy","ar-KW":"dd/MM/yyyy","en-PH":"M/d/yyyy","es-CL":"dd-MM-yyyy","ar-AE":"dd/MM/yyyy","es-UY":"dd/MM/yyyy","ar-BH":"dd/MM/yyyy","es-PY":"dd/MM/yyyy","ar-QA":"dd/MM/yyyy","es-BO":"dd/MM/yyyy","es-SV":"dd/MM/yyyy","es-HN":"dd/MM/yyyy","es-NI":"dd/MM/yyyy","es-PR":"dd/MM/yyyy","am-ET":"d/M/yyyy","tzm-Latn-DZ":"dd-MM-yyyy","iu-Latn-CA":"d/MM/yyyy","sma-NO":"dd.MM.yyyy","mn-Mong-CN":"yyyy/M/d","gd-GB":"dd/MM/yyyy","en-MY":"d/M/yyyy","prs-AF":"dd/MM/yy","bn-BD":"dd-MM-yy","wo-SN":"dd/MM/yyyy","rw-RW":"M/d/yyyy","qut-GT":"dd/MM/yyyy","sah-RU":"MM.dd.yyyy","gsw-FR":"dd/MM/yyyy","co-FR":"dd/MM/yyyy","oc-FR":"dd/MM/yyyy","mi-NZ":"dd/MM/yyyy","ga-IE":"dd/MM/yyyy","se-SE":"yyyy-MM-dd","br-FR":"dd/MM/yyyy","smn-FI":"d.M.yyyy","moh-CA":"M/d/yyyy","arn-CL":"dd-MM-yyyy","ii-CN":"yyyy/M/d","dsb-DE":"d. M. yyyy","ig-NG":"d/M/yyyy","kl-GL":"dd-MM-yyyy","lb-LU":"dd/MM/yyyy","ba-RU":"dd.MM.yy","nso-ZA":"yyyy/MM/dd","quz-BO":"dd/MM/yyyy","yo-NG":"d/M/yyyy","ha-Latn-NG":"d/M/yyyy","fil-PH":"M/d/yyyy","ps-AF":"dd/MM/yy","fy-NL":"d-M-yyyy","ne-NP":"M/d/yyyy","se-NO":"dd.MM.yyyy","iu-Cans-CA":"d/M/yyyy","sr-Latn-RS":"d.M.yyyy","si-LK":"yyyy-MM-dd","sr-Cyrl-RS":"d.M.yyyy","lo-LA":"dd/MM/yyyy","km-KH":"yyyy-MM-dd","cy-GB":"dd/MM/yyyy","bo-CN":"yyyy/M/d","sms-FI":"d.M.yyyy","as-IN":"dd-MM-yyyy","ml-IN":"dd-MM-yy","en-IN":"dd-MM-yyyy","or-IN":"dd-MM-yy","bn-IN":"dd-MM-yy","tk-TM":"dd.MM.yy","bs-Latn-BA":"d.M.yyyy","mt-MT":"dd/MM/yyyy","sr-Cyrl-ME":"d.M.yyyy","se-FI":"d.M.yyyy","zu-ZA":"yyyy/MM/dd","xh-ZA":"yyyy/MM/dd","tn-ZA":"yyyy/MM/dd","hsb-DE":"d. M. yyyy","bs-Cyrl-BA":"d.M.yyyy","tg-Cyrl-TJ":"dd.MM.yy","sr-Latn-BA":"d.M.yyyy","smj-NO":"dd.MM.yyyy","rm-CH":"dd/MM/yyyy","smj-SE":"yyyy-MM-dd","quz-EC":"dd/MM/yyyy","quz-PE":"dd/MM/yyyy","hr-BA":"d.M.yyyy.","sr-Latn-ME":"d.M.yyyy","sma-SE":"yyyy-MM-dd","en-SG":"d/M/yyyy","ug-CN":"yyyy-M-d","sr-Cyrl-BA":"d.M.yyyy","es-US":"M/d/yyyy"};

    var l=navigator.language?navigator.language:navigator['userLanguage'],y=d.getFullYear(),m=d.getMonth()+1,d=d.getDate();
    f=(l in f)?f[l]:"MM/dd/yyyy";
    function z(s){s=''+s;return s.length>1?s:'0'+s;}
    f=f.replace(/yyyy/,y);f=f.replace(/yy/,String(y).substr(2));
    f=f.replace(/MM/,z(m));f=f.replace(/M/,m);
    f=f.replace(/dd/,z(d));f=f.replace(/d/,d);
    return f;
}

使用:

shortedDate=getLocaleShortDateString(new Date(1992, 0, 7));


1
这行得通,但是不幸的是,它并没有处理一些浏览器发送的两个字母的语言代码(我喜欢用Firefox给我例如“ da”而不是“ da-DK”)。我想我只想为我要支持的语言添加两个字母的代码,但是真正很酷的解决方案是从两个字母的代码中查找并默认为某个语言文化名称。
Alvis

您好@ holden321我可以获取当地时间并将其转换为dd / MM / yyyy格式吗?我问了一个问题stackoverflow.com/questions/36226676 / ...但是任何人都帮不了我。
Avanish Kumar

1
这适用于浏览器,但不适用于NodeJS。NodeJS将始终使用上面的不同选项产生相同的格式。
多纳托

10

不能做

跨浏览器JavaScript无法使用用户在提供这种区域自定义功能的平台上选择的实际短日期格式。此外,JavaScript在涉及任何格式方面都存在巨大漏洞。看看有多少麻烦的零填充!

您可以花很多时间来获取语言设置,并获取该语言环境的典型格式。当您甚至不知道它是否是正确的语言环境(我敢打赌,国际语言标头通常不正确或不够具体),或者用户是否已将格式自定义为其他格式时,这是很多工作。

您可以尝试使用客户端VBScript(具有用于所有这些区域格式排列的功能),但这不是一个好主意,因为它是IE(即将死)的特定技术。

您也可以尝试使用Java / Flash / Silverlight来挖掘格式。这也是大量的额外工作,但可能有最大的成功机会。您希望将其缓存用于会话,以最大程度地减少开销。

希望HTML5 <time>元素可以为i18n日期/时间显示提供一些缓解。


8

如果您对<input type="date">字段有疑问,这里是获取字段value=""属性的脚本:

(new Date()).toISOString().split('T')[0]

您可以使用Intl对象(ecma-402)获得data-date-pattern=""

(new Intl.DateTimeFormat()).resolved.pattern // "M/d/y" for "en-US" in Google Chrome

最后,要将日期格式设置为当前的l10n,请执行以下操作data-date=""

(new Intl.DateTimeFormat()).format(new Date());

Polyfill:https//github.com/andyearnshaw/Intl.js/issues/129


1
标准中是否已解决对象(new Intl.DateTimeFormat())?它没有在MDN(developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)中记录,并且在Firefox中不起作用。
vcarel

2
(new Intl.DateTimeFormat()).resolved.pattern在Google Chrome浏览器中生成错误。(new Intl.DateTimeFormat()).resolvedundefined。这不是获取格式字符串的解决方案,但是您可以((new Intl.DateTimeFormat()).format(new Date());用来获取格式化的日期字符串。
Karl Wilbur '18年

现在,(new Intl.DateTimeFormat()).resolvedOptions()-将为您提供格式对象
iegik

@iegikresolvedOptions仅产生粗略数据(至少在Chrome 80上),例如。缺少年份表示的长度,日期成分的顺序,日期成分的分隔符的信息。
collapsar

7

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

Intl.DateTimeFormat对象是用于启用语言敏感的日期和时间格式的对象的构造函数。

var date = new Date(2014, 11, 31, 12, 30, 0);

var formatter = new Intl.DateTimeFormat("ru");
console.log( formatter.format(date) ); // 31.12.2014

var formatter = new Intl.DateTimeFormat("en-US");

console.log(formatter.format(date)); // 12/31/2014

您当前区域的格式:

console.log(new Intl.DateTimeFormat(Intl.DateTimeFormat().resolvedOptions().locale).
format(new Date()))

5

我不知道这样做的方法(您可以获取语​​言并尝试从中扣除语言环境)。

我尝试做一些尝试来做到这一点(仅在具有一种语言环境的Firefox上进行了测试)。只要短日期字符串中的日期包含数字,它就应该可以工作,因此它可能无法使用阿拉伯语。它可能也有其他错误,我不知道所有不同的语言环境特性,这只是一个概念...

function getShortDateFormat() {
    var d = new Date(1992, 0, 7);
    var s = d.toLocaleDateString();

    function formatReplacer(str) {
        var num = parseInt(str);
        switch (num % 100) {
            case 92:
                return str.replace(/.{1}/g, "Y");
            case 1:
                return str.length == 1 ? "mM" : "MM"
            case 7:
                return str.length == 1 ? "dD" : "DD"
        }
    }

    shortDateFormat = s.replace(/\d+/g, formatReplacer);
    return shortDateFormat;
}

getShortDateFormat();

输出格式为:

  • Y:代表年份的位数
  • dD =短日(即,尽可能使用一位数字)
  • DD =长日格式(即始终为两位数)
  • mM / MM-相同月份

因此,在我的浏览器中,您得到的shortDateformat是“ MM / DD / YYYY”。


我得到:“貂,DD日enero德YYYY”
阿尔瓦罗·冈萨雷斯

那么显然我需要做更多的工作:)(仅在Firefox 3.5 / Linux上测试)
Amitay Dobo 2010年

返回undefined日期部分的镀铬38 X11
钢脑

3

这取决于浏览器的toLocaleDateString()实现。例如,在chrome中,您将获得类似以下内容的信息:Tuesday, January DD, YYYY


2

对Mitali的响应进行了轻微修改。为更本地化的解决方案动态生成语言。

var lang= window.navigator.userLanguage || window.navigator.language;

var date = new Date();

var options = {
   weekday: "short",
   year: "numeric",
   month: "2-digit",
   day: "numeric"
};

date.toLocaleDateString(lang, options);

2

我相信您可以使用以下一种:

new Date().toLocaleDateString();

可以接受语言环境的参数:

new Date().toLocaleDateString("en-us");
new Date().toLocaleDateString("he-il");

我看到它受到chrome,IE,edge的支持,尽管结果可能会有所不同,但对我来说做得很好。


2
new Date(YOUR_DATE_STRING).toLocaleDateString(navigator.language)

〜以上答案的组合


1

没有简单的方法。如果您需要一个可靠的跨浏览器解决方案,则必须按文化构建日期和时间格式字符串的查找表。要格式化日期,请解析相应的格式字符串,从日期(即日,月,年)中提取相关部分,并将其附加在一起。

实质上,这就是Microsoft使用其AJAX库所做的事情,如@no的答案所示。


1

在寻找一种方法在所有浏览器中生成本地化但较短的日期而无需任何额外库的过程中找到了此线程,只需简单地调用即可格式化服务器生成的ISO日期。

我结合了holden321和mwrf的答案。存储函数外部的格式,因为这样比较方便,而存储当前的语言环境格式,因为它总是有用的。

该函数可以传递可以传递给日期构造函数以获取有效日期的任何内容,还可以传递语言或格式以提高灵活性。

您需要先设置格式列表,然后再进行其他操作:

var LOCALE_DATE_FORMATS = {
  // as per mwrf's list
  ...
}

var LANG = window.navigator.userLanguage || window.navigator.language;

var LOCALE_DATE_FORMAT = LOCALE_DATE_FORMATS[LANG] || 'dd/MM/yyyy';

那么函数是:

function getLocalizedDate(o, options)
{
    var res = '';
    var date = new Date(o);     

    if(o != null && !isNaN(date) && typeof(date.getFullYear) == 'function')
    {
        var d = date.getDate();
        var dd = d < 10 ? '0' + d : d;
        var M = date.getMonth() + 1;
        var MM = M < 10 ? '0' + M : M;
        var yyyy = date.getFullYear();
        var yy = new String(yyyy).substring(2);

        var format = LOCALE_DATE_FORMAT;

        if(options && options.lang)
            format = LOCALE_DATE_FORMATS[options.lang];
        else if(options && options.format)
            format = options.format;

        res = format
            .replace(/dd/g, dd)
            .replace(/d/g, d)
            .replace(/MM/g, MM)
            .replace(/M/g, M)
            .replace(/yyyy/g, yyyy)
            .replace(/yy/g, yy);
    }

    return res;
}

getLocalizedDate('2013-03-01T00:00:00', {lang:'sk-SK'}) = 1. 3. 2013

编辑:不带参数调用使用用户的语言环境,当然:

getLocalizedDate('2013-03-01T00:00:00')= 01/03/2013(对我而言)

如果您喜欢扩展原型,函数定义的第一行将变为:

Date.prototype.getLocalized = function(options)
{
  var date = this;

这样调用当然是这样的:

new Date('2013-03-01T00:00:00').getLocalized({lang:'sk-SK'}) = 1. 3. 2013

编辑:同样,不带参数调用以使用用户的语言环境:

new Date('2013-03-01T00:00:00').getLocalized()= 01/03/2013(对我而言)



-5

尝试使用Date.CultureInfo.formatPatterns.shortDate


3
这个问题要求使用javascript,而不是C#。
Izzy 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.