javascript date custom string format

Solutions on MaxInterview for javascript date custom string format by the best coders in the world

showing results for - "javascript date custom string format"
Micaela
04 Jan 2019
1var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));
2
3// request a weekday along with a long date
4var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
5console.log(new Intl.DateTimeFormat('de-DE', options).format(date));
6// → "Donnerstag, 20. Dezember 2012"
7
8// an application may want to use UTC and make that visible
9options.timeZone = 'UTC';
10options.timeZoneName = 'short';
11console.log(new Intl.DateTimeFormat('en-US', options).format(date));
12// → "Thursday, December 20, 2012, GMT"
13
14// sometimes you want to be more precise
15options = {
16  hour: 'numeric', minute: 'numeric', second: 'numeric',
17  timeZone: 'Australia/Sydney',
18  timeZoneName: 'short'
19};
20console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
21// → "2:00:00 pm AEDT"
22
23// sometimes you want to be very precise
24options.fractionalSecondDigits = 3; //number digits for fraction-of-seconds
25console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
26// → "2:00:00.200 pm AEDT"
27
28// sometimes even the US needs 24-hour time
29options = {
30  year: 'numeric', month: 'numeric', day: 'numeric',
31  hour: 'numeric', minute: 'numeric', second: 'numeric',
32  hour12: false,
33  timeZone: 'America/Los_Angeles'
34};
35console.log(new Intl.DateTimeFormat('en-US', options).format(date));
36// → "12/19/2012, 19:00:00"
37
38// to specify options but use the browser's default locale, use 'default'
39console.log(new Intl.DateTimeFormat('default', options).format(date));
40// → "12/19/2012, 19:00:00"
41
42// sometimes it's helpful to include the period of the day
43options = {hour: "numeric", dayPeriod: "short"};
44console.log(new Intl.DateTimeFormat('en-US', options).format(date));
45// → 10 at night
46