???$.fn.extend({ ???????/* ???????** 时间戳显示为【不久前,刚刚,N分钟前,N小时前,N天前,N周前,N月前,N年N月N日】的处理 ???????** eg. ???????** $(‘1517451000000‘).commonTimeStamp(); ?// 2018年02月01日 ???????** $(new Date(‘2018-02-01 15:10:00‘).getTime()).commonTimeStamp(); ?// 1分钟前 ???????*/ ???????"commonTimeStamp": function () { ???????????// 补全为13位 ???????????var arrTimestamp = (this.selector || $(this)[0] + ‘‘).split(‘‘), ???????????????zero = function (value) { ?// 数值补0方法 ???????????????????if (value < 10) { ???????????????????????return ‘0‘ + value; ???????????????????} ???????????????????return value; ???????????????}, ???????????????arrTime = { ???????????????????minute: 60000, ?????????????// 1000 * 60, ???????????????????hour: 3600000, ?????????????// 1000 * 60 * 60, ???????????????????day: 86400000, ?????????????//1000 * 60 * 60 * 24, ???????????????????week: 604800000, ???????????// 1000 * 60 * 60 * 24 * 7, ???????????????????halfamonth: 1296000000, ????// 1000 * 60 * 60 * 24 * 15, ???????????????????month: 2592000000, ?????????//1000 * 60 * 60 * 24 * 30, ???????????????????now: new Date().getTime() ???????????????}; ???????????for (var start = 0; start < 13; start++) { ???????????????if (!arrTimestamp[start]) { ???????????????????arrTimestamp[start] = ‘0‘; ???????????????} ???????????} ???????????arrTimestamp = arrTimestamp.join(‘‘) * 1; ???????????arrTime.diffValue = new Date().getTime() - arrTimestamp; ???????????// 如果本地时间反而小于变量时间 ???????????if (arrTime.diffValue < 0) { ???????????????return ‘不久前‘; ???????????} ???????????// 计算差异时间的量级 ???????????var arrTimeDiff = { ???????????????monthC: arrTime.diffValue / arrTime.month, ???????????????weekC: arrTime.diffValue / arrTime.week, ???????????????dayC: arrTime.diffValue / arrTime.day, ???????????????hourC: arrTime.diffValue / arrTime.hour, ???????????????minC: arrTime.diffValue / arrTime.minute, ???????????}; ???????????// 使用 ???????????if (arrTimeDiff.monthC > 12) { ???????????????// 超过1年,直接显示年月日 ???????????????return (function () { ???????????????????var date = new Date(arrTimestamp); ???????????????????return date.getFullYear() + ‘年‘ + zero(date.getMonth() + 1) + ‘月‘ + zero(date.getDate()) + ‘日‘; ???????????????})(); ???????????} else if (arrTimeDiff.monthC >= 1) { ???????????????return parseInt(arrTimeDiff.monthC) + "月前"; ???????????} else if (arrTimeDiff.weekC >= 1) { ???????????????return parseInt(arrTimeDiff.weekC) + "周前"; ???????????} else if (arrTimeDiff.dayC >= 1) { ???????????????return parseInt(arrTimeDiff.dayC) + "天前"; ???????????} else if (arrTimeDiff.hourC >= 1) { ???????????????return parseInt(arrTimeDiff.hourC) + "小时前"; ???????????} else if (arrTimeDiff.minC >= 1) { ???????????????return parseInt(arrTimeDiff.minC) + "分钟前"; ???????????} ???????????return ‘刚刚‘; ???????}, ???????/* ???????** 时间戳显示为【今天 上午10:10,今天 下午15:10,昨天 上午10:10,昨天 下午15:10,上午10:10,下午15:10】的处理 ???????** eg. ???????** $(‘1517451000000‘).specificTimeStamp(); ?// 今天 上午10:10 ???????** $(new Date(‘2018-02-01 15:10:00‘).getTime()).specificTimeStamp(); ?// 今天 下午15:10 ???????*/ ???????"specificTimeStamp": function () { ???????????var arrTimestamp = this.selector || $(this)[0] - 0, ???????????????parTime = new Date(arrTimestamp), ???????????????diffDay = parseInt((new Date().getTime() - arrTimestamp) / (1000 * 60 * 60 * 24)); ???????????// 使用 ???????????return ((diffDay >= 1 && diffDay <= 2) ? "昨天 " : ??????????????????????(diffDay >= 0 && diffDay <= 1) ? "今天 " : "") + ??????????????????????((parTime.getHours() / 12 > 1 ? "下午" : "上午") + ??????????????????????parTime.getHours() + ":" + parTime.getMinutes()); ???????} ???});
js ?jq插件 ?显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午
原文地址:https://www.cnblogs.com/GoCircle/p/8400498.html