js获取电脑当前时间

效果:

js获取电脑当前时间_第1张图片

//获取时间
window.onload=function(){

    //定时器每秒调用一次fnDate()
    setInterval(function(){
        fnDate();
        },1000);
}

//js 获取当前时间
function fnDate(){
    var curtTime=document.getElementById("curt-time");
    var curtDate=document.getElementById("curt-date");
    var curtDay=document.getElementById("curt-day");
    var date=new Date();
    var year=date.getFullYear();//当前年份
    var month=date.getMonth();//当前月份
    var data=date.getDate();//天
    var day =date.getDay();//周几
    var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    var hours=date.getHours();//小时
    var minute=date.getMinutes();//分
    var second=date.getSeconds();//秒

    month = month + 1;
    if (month < 10) month = "0" + month;
    if (data < 10) data = "0" + data;
    if (hours < 10) hours = "0" + hours;
    if (minute < 10) minute = "0" + minute;
    if (second < 10) second = "0" + second;

    var curtDates=year+"-"+month+"-"+data;
    var curtTimes=hours+":"+minute+":"+second;
    curtTime.innerHTML=curtTimes;
    curtDate.innerHTML=curtDates;
    curtDay.innerHTML=weekday[day];
}

 

你可能感兴趣的:(前端,获取电脑时间)