前端JS日期方法

1. 日期不足两位补 0

function appendZero(obj) {
    obj = Number(obj);
    if (obj < 10) return "0" + "" + obj;
    else return obj;
}

 2. 获取今天的年月日 , 时间格式为: 2020-07-24

function() getCurrentDay {
    return new Date().getFullYear() + ‘-‘ + appendZero(new Date().getMonth() + 1) + ‘-‘ + appendZero(new Date().getDate());
}

3. 时间戳转时间

function timestampToDate(timestamp) {
    var now = new Date(Number(timestamp)),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
    return y + "-" + appendZero(m) + "-" + appendZero(d) + " " + now.toTimeString().substr(0, 8);
}

 4. 获取指定年的月份列表 , 返回结果如:[‘2020-01‘ , ‘2020-02‘ , ‘2020-03‘ , ‘2020-04‘ , ...]

function getMonthsByYears (year) {
    var months = [];
    for (var i = 1; i <= 12; i++) {
        months.push(year + ‘-‘ + $.appendZero(i));
    }
    return months;
}

 

前端JS日期方法

上一篇:POJ 2567 Code the Tree & POJ 2568 Decode the Tree Prufer序列


下一篇:Work Management Service application in SharePoint 2016