1var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
2var today = new Date();
3
4console.log(today.toLocaleDateString("en-US")); // 9/17/2016
5console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
6console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016
1const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
2
3const options = { year: 'numeric', month: 'short', day: 'numeric' };
4
5console.log(event.toLocaleDateString('de-DE', options));
6// expected output: Donnerstag, 20. Dezember 2012
7
8console.log(event.toLocaleDateString('en-US', options));
9// US format
10
11
12// In case you only want the month
13console.log(event.toLocaleDateString(undefined, { month: 'short'}));
14console.log(event.toLocaleDateString(undefined, { month: 'long'}));
1var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
2var today = new Date();
3
4console.log(today.toLocaleDateString("en-US")); // 9/17/2016
5console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
6console.log(today.toLocaleDateString("hi-IN", options));
1const t = new Date();
2const date = ('0' + t.getDate()).slice(-2);
3const month = ('0' + (t.getMonth() + 1)).slice(-2);
4const year = t.getFullYear();
5const hours = ('0' + t.getHours()).slice(-2);
6const minutes = ('0' + t.getMinutes()).slice(-2);
7const seconds = ('0' + t.getSeconds()).slice(-2);
8const time = `${date}/${month}/${year}, ${hours}:${minutes}:${seconds}`;
9
10output: "27/04/2020, 12:03:03"
1function formatDate(date) {
2 var d = new Date(date),
3 month = '' + (d.getMonth() + 1),
4 day = '' + d.getDate(),
5 year = d.getFullYear();
6
7 if (month.length < 2)
8 month = '0' + month;
9 if (day.length < 2)
10 day = '0' + day;
11
12 return [year, month, day].join('-');
13}
14
15console.log(formatDate('Sun May 11,2014'));