欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS函数库

程序员文章站 2022-06-03 18:51:45
...

获取当前日期:形如xxxx年xx月xx日

function getDate() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var currentdate = date.getFullYear() + '年' + month + '月' + strDate + '日';
    document.getElementById("CurrentDate").innerHTML += currentdate;
}

日期格式化函数:形如2016-0814

function dateFormat() {
    var myDate = new Date();
    var today = "";
    today = myDate.getFullYear() + (((myDate.getMonth()) < 10) ? ("-0" + myDate.getMonth()) : (myDate.getMonth())) + (((myDate.getDate()) < 10) ? ("-0" + myDate.getDate()) : (myDate.getDate()));
    document.write(today);
}

时钟函数:形如2:50:56

function clock() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById("clock").innerHTML = h + ":" + m + ":" + s;
    t = setTimeout(function() {
        clock();
    }, 500);
}

function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}