其他分享
首页 > 其他分享> > 动态获取系统时间

动态获取系统时间

作者:互联网

一、方法一

// js获取当前时间
function getTime() {
    var myDate = new Date();
    var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var myToday = myDate.getDate(); //获取当前日(1-31)
    var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
    var myHour = myDate.getHours(); //获取当前小时数(0-23)
    var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
    var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
    var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    var nowTime;

    nowTime = "今天是:" + myYear + '年' + fillZero(myMonth) + '月' + fillZero(myToday) + '日' + '  ' + fillZero(
            myHour) + ':' +
        fillZero(myMinute) + ':' + fillZero(mySecond) + '  ' + week[myDay] + '  ';
    //console.log(nowTime);
    // $('.time').html(nowTime);
    document.getElementsByClassName("time")[0].innerText = nowTime;
};
// 补位
function fillZero(str) {
    var realNum;
    if (str < 10) {
        realNum = '0' + str;
    } else {
        realNum = str;
    }
    return realNum;
}
setInterval(getTime, 1000);

二、 方法二

writeCurrentDate();

function writeCurrentDate() {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    var day = now.getDay(); //得到周几
    var hour = now.getHours(); //得到小时
    var minu = now.getMinutes(); //得到分钟
    var sec = now.getSeconds(); //得到秒
    var MS = now.getMilliseconds(); //获取毫秒
    var week;
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    if (MS < 100) MS = "0" + MS;
    var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    week = arr_week[day];
    var time = "";
    time = "今天是" + year + "年" + month + "月" + date + "日" + " " + week + " " + hour + ":" + minu + ":" + sec;

    document.getElementById("shijian").innerHTML = time;
    //设置得到当前日期的函数的执行间隔时间,每1000毫秒刷新一次。
    var timer = setTimeout("writeCurrentDate()", 1000);
}

三、方法三

setInterval(function () {
    time();
}, 1000)

function time() {
    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 week = date.getDay();
    var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

    document.getElementById("shijian").innerHTML = "今天是:" + year + '年' + month + '月' + day + '日 ' + weekday[week] +
        hour + ':' + minute + ':' + second;
}

四、方法四

// 动态显示日期和时间
function clock() {
    var date = new Date();
    var year = date.getFullYear();    //年
    var month = date.getMonth() + 1;  //月
    var day = date.getDate();         //日

    var week = date.getDay(); //以数值获取周名(0-6)
    var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

    // 补位
    function buwei(value) {
        if (value < 10) {
            value = "0" + value;
        }
        return value;
    }

    var h = buwei(date.getHours());
    var m = buwei(date.getMinutes());
    var s = buwei(date.getSeconds());

    var time = year + "年" + month + "月" + day + "日" + "\xa0" + weeks[week] + "\xa0\xa0\xa0" + h + ":" + m + ":" + s;

    document.getElementById("clock").innerText = time;
}
setInterval(clock, 1000)

五、方法五

setInterval(function(){
  var today = new Date();

  function itArray() {
  this.length = itArray.arguments.length
  for (var i = 0; i < this.length; i++)
      this[i + 1] = itArray.arguments[i]
  }
  var d = new itArray("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  
  document.getElementById("today").innerHTML = "今天是:"+today.getFullYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日 "+d[today
      .getDay() + 1]+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
}, 1000)

六、方法六

function clock(){
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1): date.getMonth() + 1;
    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var week = date.getDay(); //以数值获取周名(0-6)
    var weeks = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    
    var time = year + "-" + month + "-" + day + "\xa0\xa0\xa0" + weeks[week] + "\xa0\xa0\xa0" + h + ":" + m + ":" + s;
    document.getElementById("clock").innerText = time;
}
setInterval(clock,1000);

标签:week,date,xa0,系统,month,获取,time,var,动态
来源: https://www.cnblogs.com/cdnotes/p/14824028.html