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”>點選當前顯示時間</p
button onclick=“but()”>點選</button