处理时间戳的函数封装

使用很简单,直接调用getDate(time)函数即可,参数就是你的时间戳

功能说明:根据你传入的时间戳和当前时间进行比较:返回的格式有:年-月-日、星期几、昨天、刚刚


function getDate(time){
	var now = new Date();
	var last = new Date(time);
	var nowMin =  now.getTime()/1000/60;
	var lastMin =  time/1000/60;
	var day = 1000*60*60*24;
	var lastDate = last.getFullYear() + "-" + toDB(last.getMonth()) + "-" + toDB(last.getDate());
	var weebText = ["日","一","二","三","四","五","六"];
	if(now.getFullYear() > last.getFullYear()){
		return lastDate;
	} 
	if(now.getMonth() > last.getMonth()){
		return lastDate;
	}
	if(now.getTime() - time > day*(now.getDay()+1)){
		return lastDate;
	}
	if(now.getDate() - 1 > last.getDate()){
		return "星期" + weebText[last.getDay()];
	}
	if(now.getDate() > last.getDate()){
		return "昨天";
	} 
	if(nowMin - lastMin < 1){
		return "刚刚";
	}
	if(nowMin - lastMin < 60){
		return Math.floor(nowMin - lastMin) + "分钟前";
	}
	return toDB(last.getHours()) + ":" + toDB(last.getMinutes()); 
}

猜你喜欢

转载自blog.csdn.net/lhjuejiang/article/details/80683303
今日推荐