js计算时间差

用js计算时间差,得到比较人性化的结果0+2,006 views / 2009.09.03 / 5:05 下午
我们经常看到一些文章或者评论的发表时间不是直接显示出死板的Y-m-d格式,而是算了一个时间差,如:发表于2分钟前,这样显得比较人性化,多用于一些非正式网站。下面提供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;
}
如果你得到的原始数据不是时间戳,可以采用下面的函数把字符串转换为标准时间戳, 它相当于JS版的strtotime,只不过精度不同罢了:

function getDateTimeStamp(dateStr){
 return Date.parse(dateStr.replace(/-/gi,"/"));
}















<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js计算时间差</title>
<script language="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("结束日期不能小于开始日期!");
 }
  alert("当前时间:"+now);
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;
}
</script>
</head>

<body>
<script type="text/javascript">
function getDateTimeStamp(dateStr){
 return Date.parse(dateStr.replace(/-/gi,"/"));
}
//把带有格式的时间转换 成 毫秒数
document.write(getDateTimeStamp("2011-05-22 12:12:12")+"<br><br><br><br>");

 //document.write(getDateDiff("1306220613953"));
 //通过转换后 输出 结果
 document.write(getDateDiff(getDateTimeStamp("2011-05-24 12:12:12")));
</script>
</body>
</html>

 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;
}
</script>
</head>

<body>
<script type="text/javascript">
 document.write(getDateDiff("1306220613953"));
</script>
</body>
</html>

posted on 2012-06-28 15:41  水行者  阅读(705)  评论(0编辑  收藏  举报

导航