使用Javascript语言,将时间戳转换为相似新浪微博的时间的表示方法。
要求转换规则:
1分钟之内显示为:刚刚
1小时之内显示为:N分钟前
当天之内显示为:今天 N点N分(如:今天 22:33)
昨天时间显示为:昨天 N点N分(如:昨天 10:15)
当年之内显示为:N月N日 N点N分(如:02月03日 09:33)
今年之前显示为:N年N月N日 N点N分(如:2000年09月18日 15:59)函数
自定义的转换函数:code
function timestampFormat( timestamp ) { function zeroize( num ) { return (String(num).length == 1 ? '0' : '') + num; } var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳 var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数 var curDate = new Date( curTimestamp * 1000 ); // 当前时间日期对象 var tmDate = new Date( timestamp * 1000 ); // 参数时间戳转换成的日期对象 var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate(); var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds(); if ( timestampDiff < 60 ) { // 一分钟之内 return "刚刚"; } else if( timestampDiff < 3600 ) { // 一小时前以内 return Math.floor( timestampDiff / 60 ) + "分钟前"; } else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) { return '今天' + zeroize(H) + ':' + zeroize(i); } else { var newDate = new Date( (curTimestamp - 86400) * 1000 ); // 参数中的时间戳加一天转换成的日期对象 if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d ) { return '昨天' + zeroize(H) + ':' + zeroize(i); } else if ( curDate.getFullYear() == Y ) { return zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i); } else { return Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i); } } }
参数的时间不能大于当前时间,大于当前时间会返回“刚刚”。orm
用法展现:对象
timestampFormat(1326170770); //2012年01月10日 12:46 timestampFormat(Date.parse('2016-10-11 15:26:10')/1000); //刚刚 timestampFormat(Date.parse('2016-10-11 15:10:10')/1000); //16分钟前 timestampFormat(Date.parse('2016-10-11 10:10:10')/1000); //今天10:10 timestampFormat(Date.parse('2016-10-10 10:10:10')/1000); //昨天10:10 timestampFormat(Date.parse('2016-02-10 10:10:10')/1000); //02月10日 10:10 timestampFormat(Date.parse('2012-10-10 10:10:10')/1000); //2012年10月10日 10:10
在新浪微博首页看到每条微博后边显示的时间并非标准的年-月-日格式,而是通过换算的时间差,如:发表于5分钟前、发表于“2小时前”,比起标准的时间显示格式,貌似更加直观和人性化。要实现相似功能,用JS就能够,实现方法以下:ip
//JavaScript函数: var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; function getDateDiff(dateTimeStamp){ var now = new Date().getTime(); var diffValue = now - dateTimeStamp; if(diffValue < 0){ //若日期不符则弹出窗口告之 //alert("结束日期不能小于开始日期!"); } var monthC =diffValue/month; var weekC =diffValue/(7*day); var dayC =diffValue/day; var hourC =diffValue/hour; var minC =diffValue/minute; 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; }
若你获得的时间格式不是时间戳,能够使用下面的JavaScript函数把字符串转换为时间戳, 本函数的功能至关于JS版的strtotime:字符串
//js函数代码:字符串转换为时间戳 function getDateTimeStamp(dateStr){ return Date.parse(dateStr.replace(/-/gi,"/")); }