js 根据时间,输出几分钟前,几小时前,几天前,几个月前,几年前。

这段代码展示了两个JavaScript函数,用于将时间戳转换成相对于当前时间的分钟、小时、天数前的描述。在测试中,输入的时间戳分别被正确地转换为了'2小时前'和'3天前'的格式。这两个函数可以用于网站或应用中显示动态更新的内容时间。
摘要由CSDN通过智能技术生成

代码1:

// 时间戳转多少分钟之前
function getDateDiff(dateTimeStamp) {
    // 时间字符串转时间戳
    var timestamp = new Date(dateTimeStamp).getTime();
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var year = day * 365;
    var now = new Date().getTime();
    var diffValue = now - timestamp;
    var result;
    if (diffValue < 0) {
        return;
    }
    var yearC = diffValue / year;
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (yearC >= 1) {
        result = "" + parseInt(yearC) + "年前";
    } else if (monthC >= 1) {
        result = "" + parseInt(monthC) + "月前";
    } else if (weekC >= 1) {
        result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
        result = "" + parseInt(minC) + "分钟前";
    } else
        result = "刚刚";
    return result;
}
console.log(getDateDiff("2020-12-16 12:12:12"));

测试时间:2020年12月16日14:30:21,输出结果:2小时前。

代码2:

function getDateDiff(dateStr) {
        var publishTime = getDateTimeStamp(dateStr) / 1000,
        d_seconds,
        d_minutes,
        d_hours,
        d_days,
        timeNow = parseInt(new Date().getTime() / 1000),
        d,
 
        date = new Date(publishTime * 1000),
        Y = date.getFullYear(),
        M = date.getMonth() + 1,
        D = date.getDate(),
        H = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds();
        //小于10的在前面补0
        if (M < 10) {
                M = '0' + M;
        }
        if (D < 10) {
                D = '0' + D;
        }
        if (H < 10) {
                H = '0' + H;
        }
        if (m < 10) {
                m = '0' + m;
        }
        if (s < 10) {
                s = '0' + s;
        }
 
        d = timeNow - publishTime;
        d_days = parseInt(d / 86400);
        d_hours = parseInt(d / 3600);
        d_minutes = parseInt(d / 60);
        d_seconds = parseInt(d);
 
        if (d_days > 0 && d_days < 3) {
                return d_days + '天前';
        } else if (d_days <= 0 && d_hours > 0) {
                return d_hours + '小时前';
        } else if (d_hours <= 0 && d_minutes > 0) {
                return d_minutes + '分钟前';
        } else if (d_seconds < 60) {
                if (d_seconds <= 0) {
                        return '刚刚';
                } else {
                        return d_seconds + '秒前';
                }
        } else if (d_days >= 3 && d_days < 30) {
                return M + '-' + D + ' ' + H + ':' + m;
        } else if (d_days >= 30) {
                return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
        }
}
 
function getDateTimeStamp(dateStr) {
          // 如果时间格式为2020/07/09 21:43:19.000  需要去掉.000 不然ios和firefox会有问题
return Date.parse(dateStr.replace(/-/gi, "/"));
}  

console.log(this.getDateDiff("2020-07-03 10:03:19.000"));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值