1var today = new Date();
2var dd = String(today.getDate()).padStart(2, '0');
3var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
4var yyyy = today.getFullYear();
5
6today = mm + '/' + dd + '/' + yyyy;
7document.write(today);
1// `date` is a `Date` object
2const formatYmd = date => date.toISOString().slice(0, 10);
3
4// Example
5formatYmd(new Date()); // 2020-05-06
1let date_ob = new Date();
2
3// adjust 0 before single digit date
4let date = ("0" + date_ob.getDate()).slice(-2);
5
6// current month
7let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
8
9// current year
10let year = date_ob.getFullYear();
11
12// current hours
13let hours = date_ob.getHours();
14
15// current minutes
16let minutes = date_ob.getMinutes();
17
18// current seconds
19let seconds = date_ob.getSeconds();
20
21// prints date & time in YYYY-MM-DD HH:MM:SS format
22console.log(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);