Apakah ada cara untuk mengetahui menggunakan JavaScript Format Tanggal Pendek yang digunakan di Panel Kontrol -> Pengaturan Wilayah dan Bahasa?
Saya tahu dengan menggunakan kombinasi berikut kita bisa mendapatkan format Nama Panjang Lokal
toString()
toLocaleString()
toLocaleDateString()
toLocaleTimeString()
Tetapi tidak ada fungsi langsung dalam JavaScript seperti toLocaleShortDateString()
.
Apakah ada skrip yang tersedia untuk mengetahui apa pengaturan pengguna?
Terima kasih.
javascript
datetime
lelol
sumber
sumber
Jawaban:
function getLocaleDateString() { const formats = { "af-ZA": "yyyy/MM/dd", "am-ET": "d/M/yyyy", "ar-AE": "dd/MM/yyyy", "ar-BH": "dd/MM/yyyy", "ar-DZ": "dd-MM-yyyy", "ar-EG": "dd/MM/yyyy", "ar-IQ": "dd/MM/yyyy", "ar-JO": "dd/MM/yyyy", "ar-KW": "dd/MM/yyyy", "ar-LB": "dd/MM/yyyy", "ar-LY": "dd/MM/yyyy", "ar-MA": "dd-MM-yyyy", "ar-OM": "dd/MM/yyyy", "ar-QA": "dd/MM/yyyy", "ar-SA": "dd/MM/yy", "ar-SY": "dd/MM/yyyy", "ar-TN": "dd-MM-yyyy", "ar-YE": "dd/MM/yyyy", "arn-CL": "dd-MM-yyyy", "as-IN": "dd-MM-yyyy", "az-Cyrl-AZ": "dd.MM.yyyy", "az-Latn-AZ": "dd.MM.yyyy", "ba-RU": "dd.MM.yy", "be-BY": "dd.MM.yyyy", "bg-BG": "dd.M.yyyy", "bn-BD": "dd-MM-yy", "bn-IN": "dd-MM-yy", "bo-CN": "yyyy/M/d", "br-FR": "dd/MM/yyyy", "bs-Cyrl-BA": "d.M.yyyy", "bs-Latn-BA": "d.M.yyyy", "ca-ES": "dd/MM/yyyy", "co-FR": "dd/MM/yyyy", "cs-CZ": "d.M.yyyy", "cy-GB": "dd/MM/yyyy", "da-DK": "dd-MM-yyyy", "de-AT": "dd.MM.yyyy", "de-CH": "dd.MM.yyyy", "de-DE": "dd.MM.yyyy", "de-LI": "dd.MM.yyyy", "de-LU": "dd.MM.yyyy", "dsb-DE": "d. M. yyyy", "dv-MV": "dd/MM/yy", "el-GR": "d/M/yyyy", "en-029": "MM/dd/yyyy", "en-AU": "d/MM/yyyy", "en-BZ": "dd/MM/yyyy", "en-CA": "dd/MM/yyyy", "en-GB": "dd/MM/yyyy", "en-IE": "dd/MM/yyyy", "en-IN": "dd-MM-yyyy", "en-JM": "dd/MM/yyyy", "en-MY": "d/M/yyyy", "en-NZ": "d/MM/yyyy", "en-PH": "M/d/yyyy", "en-SG": "d/M/yyyy", "en-TT": "dd/MM/yyyy", "en-US": "M/d/yyyy", "en-ZA": "yyyy/MM/dd", "en-ZW": "M/d/yyyy", "es-AR": "dd/MM/yyyy", "es-BO": "dd/MM/yyyy", "es-CL": "dd-MM-yyyy", "es-CO": "dd/MM/yyyy", "es-CR": "dd/MM/yyyy", "es-DO": "dd/MM/yyyy", "es-EC": "dd/MM/yyyy", "es-ES": "dd/MM/yyyy", "es-GT": "dd/MM/yyyy", "es-HN": "dd/MM/yyyy", "es-MX": "dd/MM/yyyy", "es-NI": "dd/MM/yyyy", "es-PA": "MM/dd/yyyy", "es-PE": "dd/MM/yyyy", "es-PR": "dd/MM/yyyy", "es-PY": "dd/MM/yyyy", "es-SV": "dd/MM/yyyy", "es-US": "M/d/yyyy", "es-UY": "dd/MM/yyyy", "es-VE": "dd/MM/yyyy", "et-EE": "d.MM.yyyy", "eu-ES": "yyyy/MM/dd", "fa-IR": "MM/dd/yyyy", "fi-FI": "d.M.yyyy", "fil-PH": "M/d/yyyy", "fo-FO": "dd-MM-yyyy", "fr-BE": "d/MM/yyyy", "fr-CA": "yyyy-MM-dd", "fr-CH": "dd.MM.yyyy", "fr-FR": "dd/MM/yyyy", "fr-LU": "dd/MM/yyyy", "fr-MC": "dd/MM/yyyy", "fy-NL": "d-M-yyyy", "ga-IE": "dd/MM/yyyy", "gd-GB": "dd/MM/yyyy", "gl-ES": "dd/MM/yy", "gsw-FR": "dd/MM/yyyy", "gu-IN": "dd-MM-yy", "ha-Latn-NG": "d/M/yyyy", "he-IL": "dd/MM/yyyy", "hi-IN": "dd-MM-yyyy", "hr-BA": "d.M.yyyy.", "hr-HR": "d.M.yyyy", "hsb-DE": "d. M. yyyy", "hu-HU": "yyyy. MM. dd.", "hy-AM": "dd.MM.yyyy", "id-ID": "dd/MM/yyyy", "ig-NG": "d/M/yyyy", "ii-CN": "yyyy/M/d", "is-IS": "d.M.yyyy", "it-CH": "dd.MM.yyyy", "it-IT": "dd/MM/yyyy", "iu-Cans-CA": "d/M/yyyy", "iu-Latn-CA": "d/MM/yyyy", "ja-JP": "yyyy/MM/dd", "ka-GE": "dd.MM.yyyy", "kk-KZ": "dd.MM.yyyy", "kl-GL": "dd-MM-yyyy", "km-KH": "yyyy-MM-dd", "kn-IN": "dd-MM-yy", "ko-KR": "yyyy-MM-dd", "kok-IN": "dd-MM-yyyy", "ky-KG": "dd.MM.yy", "lb-LU": "dd/MM/yyyy", "lo-LA": "dd/MM/yyyy", "lt-LT": "yyyy.MM.dd", "lv-LV": "yyyy.MM.dd.", "mi-NZ": "dd/MM/yyyy", "mk-MK": "dd.MM.yyyy", "ml-IN": "dd-MM-yy", "mn-MN": "yy.MM.dd", "mn-Mong-CN": "yyyy/M/d", "moh-CA": "M/d/yyyy", "mr-IN": "dd-MM-yyyy", "ms-BN": "dd/MM/yyyy", "ms-MY": "dd/MM/yyyy", "mt-MT": "dd/MM/yyyy", "nb-NO": "dd.MM.yyyy", "ne-NP": "M/d/yyyy", "nl-BE": "d/MM/yyyy", "nl-NL": "d-M-yyyy", "nn-NO": "dd.MM.yyyy", "nso-ZA": "yyyy/MM/dd", "oc-FR": "dd/MM/yyyy", "or-IN": "dd-MM-yy", "pa-IN": "dd-MM-yy", "pl-PL": "yyyy-MM-dd", "prs-AF": "dd/MM/yy", "ps-AF": "dd/MM/yy", "pt-BR": "d/M/yyyy", "pt-PT": "dd-MM-yyyy", "qut-GT": "dd/MM/yyyy", "quz-BO": "dd/MM/yyyy", "quz-EC": "dd/MM/yyyy", "quz-PE": "dd/MM/yyyy", "rm-CH": "dd/MM/yyyy", "ro-RO": "dd.MM.yyyy", "ru-RU": "dd.MM.yyyy", "rw-RW": "M/d/yyyy", "sa-IN": "dd-MM-yyyy", "sah-RU": "MM.dd.yyyy", "se-FI": "d.M.yyyy", "se-NO": "dd.MM.yyyy", "se-SE": "yyyy-MM-dd", "si-LK": "yyyy-MM-dd", "sk-SK": "d. M. yyyy", "sl-SI": "d.M.yyyy", "sma-NO": "dd.MM.yyyy", "sma-SE": "yyyy-MM-dd", "smj-NO": "dd.MM.yyyy", "smj-SE": "yyyy-MM-dd", "smn-FI": "d.M.yyyy", "sms-FI": "d.M.yyyy", "sq-AL": "yyyy-MM-dd", "sr-Cyrl-BA": "d.M.yyyy", "sr-Cyrl-CS": "d.M.yyyy", "sr-Cyrl-ME": "d.M.yyyy", "sr-Cyrl-RS": "d.M.yyyy", "sr-Latn-BA": "d.M.yyyy", "sr-Latn-CS": "d.M.yyyy", "sr-Latn-ME": "d.M.yyyy", "sr-Latn-RS": "d.M.yyyy", "sv-FI": "d.M.yyyy", "sv-SE": "yyyy-MM-dd", "sw-KE": "M/d/yyyy", "syr-SY": "dd/MM/yyyy", "ta-IN": "dd-MM-yyyy", "te-IN": "dd-MM-yy", "tg-Cyrl-TJ": "dd.MM.yy", "th-TH": "d/M/yyyy", "tk-TM": "dd.MM.yy", "tn-ZA": "yyyy/MM/dd", "tr-TR": "dd.MM.yyyy", "tt-RU": "dd.MM.yyyy", "tzm-Latn-DZ": "dd-MM-yyyy", "ug-CN": "yyyy-M-d", "uk-UA": "dd.MM.yyyy", "ur-PK": "dd/MM/yyyy", "uz-Cyrl-UZ": "dd.MM.yyyy", "uz-Latn-UZ": "dd/MM yyyy", "vi-VN": "dd/MM/yyyy", "wo-SN": "dd/MM/yyyy", "xh-ZA": "yyyy/MM/dd", "yo-NG": "d/M/yyyy", "zh-CN": "yyyy/M/d", "zh-HK": "d/M/yyyy", "zh-MO": "d/M/yyyy", "zh-SG": "d/M/yyyy", "zh-TW": "yyyy/M/d", "zu-ZA": "yyyy/MM/dd", }; return formats[navigator.language] || "dd/MM/yyyy"; }
sumber
(new Intl.DateTimeFormat()).resolved.pattern
tampaknya sudah tidak berfungsi. Saya mendapatkanUncaught TypeError: Cannot read property 'pattern' of undefined
dan saya melihat beberapa bug kromium tentang menghapusnya..resolvedOptions()
berfungsi, tetapi tidak termasuk polanya.Saya pikir ini adalah cara terbaik untuk melakukannya ...
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.. );
Lebih Banyak Opsi dan contohnya ....
Option Values 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'
sumber
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; }
menggunakan:
shortedDate=getLocaleShortDateString(new Date(1992, 0, 7));
Tampilkan cuplikan kode
shortedDate = getLocaleShortDateString(new Date(1992, 0, 7)); console.log(shortedDate); 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; }
sumber
Tidak bisa dilakukan.
JavaScript lintas browser tidak dapat menggunakan format tanggal pendek aktual yang dipilih oleh pengguna pada platform yang menawarkan penyesuaian regional tersebut. Selain itu, JavaScript memiliki lubang besar dalam hal pemformatan apa pun. Lihat betapa repotnya zero-padding!
Anda dapat berusaha keras untuk mendapatkan pengaturan bahasa, dan mendapatkan format umum untuk lokal itu. Itu banyak pekerjaan ketika Anda bahkan tidak tahu apakah itu lokal yang benar (saya berani bertaruh bahwa tajuk bahasa internasional seringkali salah atau tidak cukup spesifik), atau jika pengguna telah menyesuaikan formatnya dengan sesuatu yang lain.
Anda dapat mencoba menggunakan klien VBScript (yang memiliki fungsi untuk semua permutasi pemformatan regional ini), tetapi itu bukan ide yang baik karena ini adalah teknologi khusus IE yang sekarat (mati?).
Anda juga dapat mencoba menggunakan Java / Flash / Silverlight untuk menggali formatnya. Ini juga merupakan pekerjaan ekstra yang besar, tetapi mungkin memiliki peluang terbaik untuk sukses. Anda ingin menyimpannya untuk sesi untuk meminimalkan overhead.
Mudah-mudahan elemen <time> HTML5 akan memberikan kelegaan untuk tampilan tanggal / waktu i18n.
sumber
Jika Anda memiliki pertanyaan tentang
<input type="date">
field, berikut ini script untuk mendapatkanvalue=""
atribut file :(new Date()).toISOString().split('T')[0]
Anda dapat menggunakan objek Intl ( ecma-402 ) untuk mendapatkan
data-date-pattern=""
:(new Intl.DateTimeFormat()).resolved.pattern // "M/d/y" for "en-US" in Google Chrome
Dan pada akhirnya, format tanggal dalam l10n saat ini,
data-date=""
:(new Intl.DateTimeFormat()).format(new Date());
Polyfill: https://github.com/andyearnshaw/Intl.js/issues/129
sumber
(new Intl.DateTimeFormat()).resolved.pattern
menghasilkan kesalahan di Google Chrome.(new Intl.DateTimeFormat()).resolved
adalahundefined
. Ini bukan solusi untuk mendapatkan string format, tetapi Anda bisa menggunakan((new Intl.DateTimeFormat()).format(new Date());
untuk mendapatkan string tanggal yang diformat.(new Intl.DateTimeFormat()).resolvedOptions()
- akan memberi Anda format objekresolvedOptions
menghasilkan data kasar (setidaknya di Chrome 80), mis. info tentang panjang representasi tahun, urutan komponen tanggal, pemisah komponen tanggal semuanya hilang.https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/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
format zona Anda saat ini:
console.log(new Intl.DateTimeFormat(Intl.DateTimeFormat().resolvedOptions().locale). format(new Date()))
sumber
Saya tidak tahu cara untuk melakukannya (Anda bisa memahami bahasanya dan mencoba mengurangi lokal dari situ).
Saya mencoba memasak sesuatu untuk dicoba dan melakukannya (hanya diuji di Firefox dengan satu lokasi). harus berfungsi selama string tanggal pendek menyertakan tanggal sebagai digit, jadi mungkin gagal untuk, katakanlah, arabic. Mungkin ada bug lain juga, saya tidak tahu semua kekhasan lokal yang berbeda, ini hanya sebuah konsep ...
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();
Format yang dihasilkan adalah:
Jadi di browser saya, format shortDate yang Anda dapatkan adalah "MM / DD / YYYY".
sumber
Ini tergantung pada
toLocaleDateString()
implementasi browser . Misalnya di chrome Anda akan mendapatkan sesuatu seperti:Tuesday, January DD, YYYY
sumber
Saya yakin Anda bisa menggunakan yang ini:
new Date().toLocaleDateString();
Yang dapat menerima parameter untuk lokal:
new Date().toLocaleDateString("en-us"); new Date().toLocaleDateString("he-il");
Saya melihatnya didukung oleh chrome, IE, edge, meskipun hasilnya mungkin berbeda-beda, ini cukup bagus untuk saya.
sumber
Sedikit modifikasi pada respon Mitali. Untuk menghasilkan bahasa secara dinamis untuk solusi yang lebih terlokalisasi.
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);
sumber
new Date(YOUR_DATE_STRING).toLocaleDateString(navigator.language)
~ kombinasi jawaban di atas
sumber
Tidak ada cara yang mudah. Jika Anda menginginkan solusi lintas-browser yang andal, Anda harus membuat tabel pencarian tanggal, dan string format waktu, berdasarkan budaya. Untuk memformat tanggal, parsing string format yang sesuai, ekstrak bagian yang relevan dari tanggal, yaitu hari, bulan, tahun, dan tambahkan bersama-sama.
Ini pada dasarnya adalah apa yang Microsoft lakukan dengan perpustakaan AJAX mereka, seperti yang ditunjukkan pada jawaban @ no.
sumber
Menemukan utas ini saat mencari cara untuk menghasilkan tanggal yang dilokalkan tetapi pendek di semua browser tanpa pustaka tambahan, hanya panggilan sederhana untuk memformat tanggal ISO yang dihasilkan server.
Saya pergi dengan kombinasi jawaban holden321 dan mwrf. Menyimpan format di luar fungsi karena lebih mudah ditangani dan menyimpan format lokal saat ini karena selalu berguna.
Fungsi ini dapat mengirimkan apa saja yang dapat diteruskan ke konstruktor tanggal untuk mendapatkan tanggal yang valid, dan juga bahasa atau format untuk fleksibilitas.
Anda perlu menyiapkan daftar format dan beberapa hal lainnya terlebih dahulu:
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';
Maka fungsinya adalah:
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
EDIT: Panggil tanpa argumen untuk menggunakan lokal pengguna, tentu saja:
getLocalizedDate('2013-03-01T00:00:00')
=01/03/2013
(untuk saya)Jika Anda penggemar perluasan prototipe, baris pertama definisi fungsi menjadi:
Date.prototype.getLocalized = function(options) { var date = this;
Disebut tentu saja seperti ini:
new Date('2013-03-01T00:00:00').getLocalized({lang:'sk-SK'})
=1. 3. 2013
EDIT: Sekali lagi, panggil tanpa argumen untuk menggunakan lokal pengguna:
new Date('2013-03-01T00:00:00').getLocalized()
=01/03/2013
(untuk saya)sumber
Coba ini:
new Date().toLocaleFormat("%x");
Semua format untuk fungsi ini dapat ditemukan di sini: http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
sumber
Coba gunakan Date.CultureInfo.formatPatterns.shortDate
sumber