时间转换(今天 昨天 超过两天)

dateFormat: function(dateStr) {
Date.prototype.Format = function(fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
S: this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1
? o[k]
: ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;};
var result = "";
dateStr = dateStr.replace(/-/g, "/");
var date = new Date(dateStr);
//判断是不是今天
var day1 = new Date();

day1.setDate(day1.getDate());
var s1 = day1.Format("yyyy-MM-dd");
var dateStr1 = date.Format("yyyy-MM-dd");
if (dateStr1 == s1) {
result = "今天 " + date.Format("hh:mm");
} else {
//昨天的时间
var day2 = new Date();
day2.setDate(day2.getDate() - 1);
var s2 = day2.Format("yyyy-MM-dd");
if (dateStr1 == s2) {
result = "昨天 " + date.Format("hh:mm");
} else {
//前天的时间
var day3 = new Date();
day3.setDate(day3.getDate() - 2);
var s3 = day3.Format("yyyy-MM-dd");
if (dateStr1 == s3) {
result = "前天 " + date.Format("hh:mm");
} else {
result = date.Format("MM/dd hh:mm");
}
}
}
return result;
},
posted @ 2019-07-31 16:38  陈健儿  阅读(253)  评论(0编辑  收藏  举报