toLocaleString-date.js (1829B)
1 if (typeof Intl === "object") { 2 const localeSep = [,,].toLocaleString(); 3 4 const date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0)); 5 6 assertEq([date].toLocaleString("en-us", {timeZone: "UTC"}), "12/12/2012, 3:00:00 AM"); 7 assertEq([date].toLocaleString(["de", "en"], {timeZone: "UTC"}), "12.12.2012, 03:00:00"); 8 assertEq([date].toLocaleString("th-th", {timeZone: "UTC"}), "12/12/2555 03:00:00"); 9 assertEq([date].toLocaleString("th-th-u-nu-thai", {timeZone: "UTC"}), "๑๒/๑๒/๒๕๕๕ ๐๓:๐๐:๐๐"); 10 11 const sampleValues = [ 12 date, new Date(0), 13 ]; 14 const sampleLocales = [ 15 void 0, 16 "en", 17 "th-th-u-nu-thai", 18 "ja-jp", 19 "ar-ma-u-ca-islamicc", 20 ["tlh", "de"], 21 ]; 22 const numericFormatOptions = { 23 timeZone: "UTC", 24 year: "numeric", month: "numeric", day: "numeric", 25 hour: "numeric", minute: "numeric", second: "numeric", 26 }; 27 const longFormatOptions = { 28 timeZone: "UTC", 29 year: "numeric", month: "long", day: "numeric", 30 hour: "numeric", minute: "numeric", second: "numeric" 31 }; 32 const sampleOptions = [ 33 {timeZone: "UTC"}, 34 longFormatOptions, 35 ]; 36 37 for (let locale of sampleLocales) { 38 for (let options of sampleOptions) { 39 let dtfOptions; 40 if (options === longFormatOptions) { 41 dtfOptions = longFormatOptions; 42 } else { 43 dtfOptions = numericFormatOptions; 44 } 45 let dtf = new Intl.DateTimeFormat(locale, dtfOptions); 46 let expected = sampleValues.map(dtf.format).join(localeSep); 47 assertEq(sampleValues.toLocaleString(locale, options), expected); 48 } 49 } 50 } 51 52 if (typeof reportCompare === "function") 53 reportCompare(true, true);