JS日期计算

练练手

/**
 * 获取下一个日期(未处理时分秒)
 * @date 2024-03-20
 * @param {string|Date} baseDate 待加减的日期
 * @param {*|Object|Object[]|function(newDate:Date,baseDate:*|string|Date):Object} [way] 对象,key为period,value为increment
 * @example
 * calcNextDate(1)               // returns 2024-03-21
 * calcNextDate(new Date(),1)    // returns 2024-03-21
 * calcNextDate('2024-03-20',1)  // returns 2024-03-21
 * calcNextDate({1:1,0:1})     // returns 2024-04-21
 * calcNextDate({y:1,1:2,0:3}) // returns 2025-05-23
 * // 可定义dic, 索引为0的元素表示"日", 为1的表示"月",为2的表示"年". 默认dic为"dmy"
 * calcNextDate('2024-03-20',{yyyy:1,mm:2,dd:3
 *  , dic:['dd','mm','yyyy']}) // returns 2025-05-23
 * calcNextDate('2024-03-20',{yyyy:1,mm:2,dd:3
 *  , dic:['yyyy','mm','dd']}) // returns 2027-05-21
 * // 可传数组
 * calcNextDate('2024-03-20',[
 *     {period:0,increment:1}
 *    ,{period:1,increment:1}
 *    ,{period:1,increment:1}
 *    ,{period:0,increment:1}
 *    ,{period:2,increment:1}
 *    ,{period:0,increment:1}])// returns 2025-05-23
 * // 可传函数
 * calcNextDate('2024-03-20',function () {
 *     return {1:2,0:3,2:1}
 * })                          // returns 2025-05-23
 * calcNextDate('2024-03-20',()=>({1:2,0:3,2:1})) // returns 2025-05-23
 * @return {Date}
 */
function calcNextDate(baseDate, way){
    var newDate, newWay;
    var firstType = typeof baseDate, 
        firstIsString = firstType === 'string', 
        firstIsDate = baseDate instanceof Date;
    if (baseDate && firstIsString) {
        newDate = new Date(baseDate.replace(/-/g,'/'));
    }
    else if (baseDate && firstIsDate) {
        newDate = new Date(baseDate.getTime());
    }
    else if (baseDate != null && way == null && !firstIsString && !firstIsDate) {
        way = baseDate;
        baseDate = new Date();
        newDate = baseDate;
    }
    else {
        newDate = new Date();
    }
    if (way != null && Array.isArray(way) && way.length>0) {
        newWay = way.reduce(function(c,v){
            c[v.period] = (c[v.period] == null ? 0 : c[v.period]) + (v.increment|0);
            return c
        }, {});
    }
    else if (way != null && typeof way === 'function') {
        newWay = way.call(null, newDate, baseDate)
    }
    else if (way != null && typeof way === 'object' && way.toString() === '[object Object]') {
        newWay = JSON.parse(JSON.stringify(way));
    }
    else if (way != null && typeof way === 'number') {
        newWay = {0:way|0};
    }
    else {
        newWay = {0:1};
    }
    var f = {
        0:function (date,inc) {
            date.setDate(date.getDate()+inc);
            return date;
        }
        ,1:function (date,inc) {
            date.setMonth(date.getMonth()+inc);
            return date;
        }
        ,2:function (date,inc) {
            date.setFullYear(date.getFullYear()+inc);
            return date;
        }
        ,'-1':function (date) {
            return date;
        }
    };
    var dic = newWay['dic']||'dmy';
    delete newWay['dic'];
    var keys = Object.keys(newWay);
    return keys.reduce(function (c, k) {
        return (f[k]||f[dic.indexOf(k)])(c,newWay[k])
    }, newDate)
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值