Javascript 将时间戳显示为“多久前”

//JavaScript函数:
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var month = day * 30;
            var now = new Date().getTime();

            function getDateDiff(dateTimeStamp) {
                var diffValue = now - dateTimeStamp;
                if (diffValue < 0) {
                    //若日期不符则弹出窗口告之
                    alert("结束日期不能小于开始日期!");
                }
                var monthComment = diffValue / month;
                var weekComment = diffValue / (7 * day);
                var dayComment = diffValue / day;
                var hourComment = diffValue / hour;
                var minComment = diffValue / minute;
                if (monthComment >= 1) {
                    result = parseInt(monthComment) + "个月前";
                } else if (weekComment >= 1) {
                    result = parseInt(weekComment) + "周前";
                } else if (dayComment >= 1) {
                    result = parseInt(dayComment) + "天前";
                } else if (hourComment >= 1) {
                    result = parseInt(hourComment) + "个小时前";
                } else if (minComment >= 1) {
                    result = parseInt(minComment) + "分钟前";
                }else
                    result = "刚刚";
                return result;
            }

ps:
Date.parse(dateStr.replace(/-/gi,"/"));//js函数代码:字符串转换为时间戳


你可能感兴趣的:(Javascript 将时间戳显示为“多久前”)