js时间戳返回差值天,月,年

计算时间戳差值,天

function calculateDiffTime_day(start_time) {
    // 取整后没有值则返回false
    if(!parseInt(start_time)) return false;
    // 拿到当前时间
    let endTime = Math.round(new Date() / 1000);

    // 拿到当前时间与传值的差值
    let timeDiff = endTime - start_time
    // 取整算出天数
    return parseInt(timeDiff / 86400);
    // let day = parseInt(timeDiff / 86400);
    // let hour = parseInt((timeDiff % 86400) / 3600);
    // let minute = parseInt((timeDiff % 3600) / 60);

    // day = day?(day+'天'):'';
    // hour = hour?(hour+"时"):'';
    // minute = minute?(minute+"分"):'';
    // return day + hour + minute;
}

注:天数差值借鉴于:

https://blog.csdn.net/qq_39816586/article/details/90720542

计算时间戳差值,月

function calculateDiffTime_month(t) {
    // 年
    // 注:t * 1000,转为时间对象(js的时间戳是毫秒数)
    let oy = new Date(t * 1000).getFullYear();
    // 月
    let om = new Date(t * 1000).getMonth()+1;
    // 今天年
    let ty = new Date().getFullYear();
    // 今天月
    let tm = new Date().getMonth()+1;

    // 距今月数
    return (ty-oy)*12+(tm-om);
}

注:月数差值借鉴于:

https://zhidao.baidu.com/question/1580801752562719340.html

计算时间戳差值,年

function calculateDiffTime_year(t) {
    // 年
    // 注:t * 1000,转为时间对象(js的时间戳是毫秒数)
    let oy = new Date(t * 1000).getFullYear();
    // 今天年
    let ty = new Date().getFullYear();

    // 距今月数
    return ty-oy;
}

根据时间戳返回星期几

function getWeek(t) {
    t = new Date(parseInt(t) * 1000);

    switch(t.getDay()) {
        case 0:
            return "星期日";
        case 1:
            return "星期一";
        case 2:
            return "星期二";
        case 3:
            return "星期三";
        case 4:
            return "星期四";
        case 5:
            return "星期五";
        case 6:
            return "星期六";
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值