获取时间对象 new Date
//获取当前时间
// 1.获取时间对象
var myDate = new Date ();
// console.log(myDate);
//2.获取时间对象上的年月日、时分秒
let y = myDate.getFullYear();
let m = myDate.getMonth()+1;
let d = myDate.getDate()
let h =myDate.getHours()
let ms =myDate.getMinutes()
let s = myDate.getSeconds()
// console.log(y,m,d,h,ms,s);
// 希望输出结果为: 2022年02月28日 19时46分26秒
// 当 时 分 秒 月 日是个位数的时候需要在前面添加 0
// console.log(typeof 1);
let arr =[m,d,h,ms,s ]
for (var i=0;i<arr.length;i++){
if(arr[i]<10){
arr[i] = '0' +arr[i]
}
}
let btn = document.getElementById(['btn'])
btn.onclick=function(){
alert('当前时间是'+y + '年'+ arr[0] +'月' +arr[1] +'日 '+arr[2] + "时" +arr[3]+'分'+arr[4] + '秒');
}