使用JavaScript获取语言环境短date格式
无论如何,我们可以使用JavaScript在控制面板 – >区域和语言设置中使用短date格式吗?
我知道使用下面的组合,我们可以得到区域设置长名称格式
toString() toLocaleString() toLocaleDateString() toLocaleTimeString()
但是JavaScript中没有像toLocaleShortDateString()
这样的直接函数。
是否有任何脚本可以找出用户设置是什么?
谢谢。
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" : "dMyyyy", "da-DK" : "dd-MM-yyyy", "de-DE" : "dd.MM.yyyy", "el-GR" : "d/M/yyyy", "en-US" : "M/d/yyyy", "fi-FI" : "dMyyyy", "fr-FR" : "dd/MM/yyyy", "he-IL" : "dd/MM/yyyy", "hu-HU" : "yyyy. MM. dd.", "is-IS" : "dMyyyy", "it-IT" : "dd/MM/yyyy", "ja-JP" : "yyyy/MM/dd", "ko-KR" : "yyyy-MM-dd", "nl-NL" : "dM-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" : "dMyyyy", "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" : "dMyyyy", "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" : "dMyyyy", "sv-FI" : "dMyyyy", "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" : "dMyyyy", "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" : "dMyyyy", "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" : "dM-yyyy", "ne-NP" : "M/d/yyyy", "se-NO" : "dd.MM.yyyy", "iu-Cans-CA" : "d/M/yyyy", "sr-Latn-RS" : "dMyyyy", "si-LK" : "yyyy-MM-dd", "sr-Cyrl-RS" : "dMyyyy", "lo-LA" : "dd/MM/yyyy", "km-KH" : "yyyy-MM-dd", "cy-GB" : "dd/MM/yyyy", "bo-CN" : "yyyy/M/d", "sms-FI" : "dMyyyy", "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" : "dMyyyy", "mt-MT" : "dd/MM/yyyy", "sr-Cyrl-ME" : "dMyyyy", "se-FI" : "dMyyyy", "zu-ZA" : "yyyy/MM/dd", "xh-ZA" : "yyyy/MM/dd", "tn-ZA" : "yyyy/MM/dd", "hsb-DE" : "d. M. yyyy", "bs-Cyrl-BA" : "dMyyyy", "tg-Cyrl-TJ" : "dd.MM.yy", "sr-Latn-BA" : "dMyyyy", "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" : "dMyyyy.", "sr-Latn-ME" : "dMyyyy", "sma-SE" : "yyyy-MM-dd", "en-SG" : "d/M/yyyy", "ug-CN" : "yyyy-Md", "sr-Cyrl-BA" : "dMyyyy", "es-US" : "M/d/yyyy" }; return formats[navigator.language] || 'dd/MM/yyyy'; }
我认为这是做到这一点的最好方法
var date = new Date(); var options = { weekday: "short", year: "numeric", month: "2-digit", day: "numeric" }; 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
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":"dMyyyy","da-DK":"dd-MM-yyyy","de-DE":"dd.MM.yyyy","el-GR":"d/M/yyyy","en-US":"M/d/yyyy","fi-FI":"dMyyyy","fr-FR":"dd/MM/yyyy","he-IL":"dd/MM/yyyy","hu-HU":"yyyy. MM. dd.","is-IS":"dMyyyy","it-IT":"dd/MM/yyyy","ja-JP":"yyyy/MM/dd","ko-KR":"yyyy-MM-dd","nl-NL":"dM-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":"dMyyyy","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":"dMyyyy","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":"dMyyyy","sv-FI":"dMyyyy","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":"dMyyyy","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":"dMyyyy","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":"dM-yyyy","ne-NP":"M/d/yyyy","se-NO":"dd.MM.yyyy","iu-Cans-CA":"d/M/yyyy","sr-Latn-RS":"dMyyyy","si-LK":"yyyy-MM-dd","sr-Cyrl-RS":"dMyyyy","lo-LA":"dd/MM/yyyy","km-KH":"yyyy-MM-dd","cy-GB":"dd/MM/yyyy","bo-CN":"yyyy/M/d","sms-FI":"dMyyyy","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":"dMyyyy","mt-MT":"dd/MM/yyyy","sr-Cyrl-ME":"dMyyyy","se-FI":"dMyyyy","zu-ZA":"yyyy/MM/dd","xh-ZA":"yyyy/MM/dd","tn-ZA":"yyyy/MM/dd","hsb-DE":"d. M. yyyy","bs-Cyrl-BA":"dMyyyy","tg-Cyrl-TJ":"dd.MM.yy","sr-Latn-BA":"dMyyyy","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":"dMyyyy.","sr-Latn-ME":"dMyyyy","sma-SE":"yyyy-MM-dd","en-SG":"d/M/yyyy","ug-CN":"yyyy-Md","sr-Cyrl-BA":"dMyyyy","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));
无法完成。
跨浏览器的JavaScript没有办法在提供这种区域定制的平台上使用用户select的实际的短date格式。 此外,JavaScript在任何forms的格式方面都有巨大的漏洞。 看看有多less麻烦零填充!
您可以花费很多时间来获取语言设置,并获取该语言环境的典型格式。 当你甚至不知道它是否是正确的语言环境(我敢打赌,国际语言头文件通常是不正确的或不够具体的),或者如果用户已经将格式定制为别的东西的时候,这是很多工作。
您可以尝试使用客户端VBScript(它具有所有这些区域格式排列的function),但是这不是一个好主意,因为它是一个临终的(死了的)特定于IE的技术。
您也可以尝试使用Java / Flash / Silverlight来挖掘格式。 这也是一个额外的工作,但可能有最好的成功机会。 你想要caching它的会话,以尽量减less开销。
希望HTML5 <time>元素将为国际date/时间显示提供一些缓解。
我不知道有办法做到这一点(你可以得到的语言,并试图从中扣除地区)。
我试图做一些尝试,做到这一点(只在Firefoxtesting与一个区域设置)。 应该工作,只要短datestring包含date为数字,所以它可能会失败,例如,阿拉伯语。 它也可能有其他的错误,我不知道所有不同的语言环境的特点,这只是一个概念…
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 =短date(即尽可能只使用一个数字)
- DD =长日格式(即总是两个数字)
- 毫米/毫米 – 相同的月份
所以在我的浏览器中,你得到的shortDate格式是“MM / DD / YYYY”。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
Intl.DateTimeFormat对象是启用语言敏感的date和时间格式的对象的构造函数。
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) ); // 31.12.2014
当前区域的格式:
console.log( new Intl.DateTimeFormat(Intl.DateTimeFormat().resolvedOptions().locale). format(new Date()))
这取决于浏览器的toLocaleDateString()实现。 例如在Chrome中,您将得到如下所示的内容:“1月2日星期二,YYYY”
如果你有关于<input type="date">
字段的问题,这里是获取字段value=""
脚本value=""
属性:
(new Date()).toISOString().split('T')[0]
您可以使用Intl对象来获取data-date-pattern=""
:
(new Intl.DateTimeFormat()).resolved.pattern // "M/d/y" for "en-US" in Google Chrome
而finnaly,要在当前的l10n格式化date, data-date=""
:
(new Intl.DateTimeFormat()).format(new Date());
填充: https : //github.com/andyearnshaw/Intl.js/issues/129
没有简单的方法。 如果你想要一个可靠的,跨浏览器的解决scheme,你必须build立一个date和时间格式string的查找表,文化。 要格式化date,parsing相应的格式string,从date(即日,月,年)中提取相关部分,并将其附加在一起。
这基本上就是微软用他们的AJAX库做的,如@ no的答案所示。
find这个线程,同时寻找一种方法来生成所有浏览器中的本地化,但没有任何额外的库,只是一个简单的调用格式化服务器生成的ISOdate的date。
我结合了holden321和mwrf的答案。 存储函数外部的格式,因为这样更方便,并存储当前的语言环境格式,因为它总是有用的。
该函数可以传递任何可以传递给date构造函数以获取有效date的信息,也可以是灵活的语言或格式。
您需要首先设置格式列表和其他一些事情:
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是:
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
(对我来说)
对Mitali的回应稍作修改。 为更加本地化的解决schemedynamic生成语言。
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);
尝试使用Date.CultureInfo.formatPatterns.shortDate
我相信你可以使用这个:new Date()。toLocaleDateString();
哪些可以接受区域设置的参数:new Date()。toLocaleDateString(“en-us”); new Date()。toLocaleDateString(“he-il”);
我看到它是由铬,IE,边缘支持,虽然结果可能会有所不同,它对我来说是一个很好的工作。
尝试这个:
new Date().toLocaleFormat("%x");
这个function的所有格式可以在这里find: http : //pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html