1var today = new Date();
2var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
3
1var today = new Date();
2var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
3var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
4var dateTime = date+' '+time;
5
1var today = new Date();
2var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
3//var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
4var dateTime = date+' '+time;
5The dateTime variable contains result as:
6
72018-8-3 //11:12:40
1var today = new Date().toLocaleDateString(undefined, {
2 day: '2-digit',
3 month: '2-digit',
4 year: 'numeric',
5 hour: '2-digit',
6 minute: '2-digit',
7 second: '2-digit'
8})
9
1function getDateTime() {
2 var now = new Date();
3 var year = now.getFullYear();
4 var month = now.getMonth()+1;
5 var day = now.getDate();
6 var hour = now.getHours();
7 var minute = now.getMinutes();
8 var second = now.getSeconds();
9 if(month.toString().length == 1) {
10 month = '0'+month;
11 }
12 if(day.toString().length == 1) {
13 day = '0'+day;
14 }
15 if(hour.toString().length == 1) {
16 hour = '0'+hour;
17 }
18 if(minute.toString().length == 1) {
19 minute = '0'+minute;
20 }
21 if(second.toString().length == 1) {
22 second = '0'+second;
23 }
24 var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
25 return dateTime;
26 }
27
28 // example usage: realtime clock
29 setInterval(function(){
30 currentTime = getDateTime();
31 document.getElementById("digital-clock").innerHTML = currentTime;
32 }, 1000);