zoukankan      html  css  js  c++  java
  • js 类似发微博或者微信朋友圈的时间显示 刚刚 几天前

    /*
    ** 时间戳显示为多少分钟前,多少天前的处理
    ** eg.
    ** console.log(dateDiff(1411111111111));  // 2014年09月19日
    ** console.log(dateDiff(1481111111111));  // 9月前
    ** console.log(dateDiff(1499911111111));  // 2月前
    ** console.log(dateDiff(1503211111111));  // 3周前
    ** console.log(dateDiff(1505283100802));  // 1分钟前
    ** 参数timestamp自动补全13位数值(不到毫秒的)。 */ var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for (var start = 0; start < 13; start++) { if (!arrTimestamp[start]) { arrTimestamp[start] = '0'; } } timestamp = arrTimestamp.join('') * 1; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; var now = new Date().getTime(); var diffValue = now - timestamp; // 如果本地时间反而小于变量时间 if (diffValue < 0) { return '不久前'; } // 计算差异时间的量级 var monthC = diffValue / month; var weekC = diffValue / (7 * day); var dayC = diffValue / day; var hourC = diffValue / hour; var minC = diffValue / minute; // 数值补0方法 var zero = function (value) { if (value < 10) { return '0' + value; } return value; }; // 使用 if (monthC > 12) { // 超过1年,直接显示年月日 return (function () { var date = new Date(timestamp); return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日'; })(); } else if (monthC >= 1) { return parseInt(monthC) + "月前"; } else if (weekC >= 1) { return parseInt(weekC) + "周前"; } else if (dayC >= 1) { return parseInt(dayC) + "天前"; } else if (hourC >= 1) { return parseInt(hourC) + "小时前"; } else if (minC >= 1) { return parseInt(minC) + "分钟前"; } return '刚刚'; };

      

    群里的一个小伙伴(NightEagle)写的,共享出来了,我就做个记录。

    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) {
            return Date.parse(dateStr.replace(/-/gi, "/"));
    }  
    

      

  • 相关阅读:
    tuple 元组及字典dict
    day 49 css属性补充浮动 属性定位 抽屉作业
    day48 选择器(基本、层级 、属性) css属性
    day47 列表 表单 css初识
    day 46 http和html
    day 45索引
    day 44 练习题讲解 多表查询
    day 40 多表查询 子查询
    day39 表之间的关联关系、 补充 表操作总结 where 、group by、
    day38 数据类型 约束条件
  • 原文地址:https://www.cnblogs.com/tonnytong/p/6065527.html
Copyright © 2011-2022 走看看