js实现点击实现当前时间格式为2019/01/12/14:52:10周六

function but(){
var date= new Date(); //获取日期
var year=date.getFullYear(); //获取年份
var month=date.getMonth()+1; //获取月份
var day=date.getDate(); //获取天
var hour=date.getHours(); //获取时
var minute=date.getMinutes(); //获取分
var second=date.getSeconds(); //获取秒
var weekday=date.getDay(); //获取周
var week=new Array(7); //数组
week[0]=‘周日’;
week[1]=‘周一’;
week[2]=‘周二’;
week[3]=‘周三’;
week[4]=‘周四’;
week[5]=‘周五’;
week[6]=‘周六’;
if (month<10) {
month=‘0’+month;
}
if (day<10) {
day=‘0’+day;
}
if (hour<10) {
hour=‘0’+hour;
}
if (minute<10) {
minute=‘0’+minute;
}
if (second<10) {
second=‘0’+second;
}
var time=year+’/’+month+’/’+day+’/’+hour+’:’+minute+’:’+second+ week[weekday]; //时间格式+字符串链接
document.getElementById(‘demo’).innerHTML=time;
setInterval(but,1000)
}
p id=“demo”>点击当前显示时间 button οnclick=“but()”>点击

你可能感兴趣的:(js实现点击实现当前时间格式为2019/01/12/14:52:10周六)