首页 > Web开发 > 详细

前端JS日期方法

时间:2020-07-24 12:35:48      阅读:74      评论:0      收藏:0      [点我收藏+]

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日期方法

原文:https://www.cnblogs.com/asune/p/13370792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!