<button>show time</button>
<div>a time</div>
<p></p>
<script>
function getDate() {
var date = new Date();
var year = date.getFullYear();
var mon = date.getMonth() + 1;
var date2 = date.getDate();
var hours = date.getHours();
var minu = date.getMinutes();
var sec = date.getSeconds();
// let day=date.getDay();
mon = mon < 10 ? '0' + mon : mon;
date2 = date2 < 10 ? '0' + date2 : date2;
hours = hours < 10 ? '0' + hours : hours;
minu = minu < 10 ? '0' + minu : minu;
sec = sec < 10 ? '0' + sec : sec;
return year + '-' + mon + '-' + date2 + ' ' + hours + ':' + minu + ':' + sec;
}
var btn = document.querySelector('button');
var div = document.querySelector('div');
btn.onclick = function () {
div.innerText = getDate();
}
var p = document.querySelector('p');
// while (true) {
// for (let i = 0; i < 100000000; i++) ;
p.innerText = getDate();
// for (let i = 0; i < 100000000; i++) ;
}
</script>