日期处理工具类
getTimestamp = (dateStr) => {
return Date.parse(new Date(dateStr));
};
dateFmt = (fmt = "yyyy-MM-dd", date = new Date()) => {
let dateType = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"H+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for (let k in dateType)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (1 === RegExp.$1.length) ?
(dateType[k]) : (("00" + dateType[k]).substr(("" + dateType[k]).length)));
return fmt;
};
isYesterday = (date = new Date()) => {
let currentDate = (new Date());
let today = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).getTime();
let yesterday = new Date(today - 24 * 3600 * 1000).getTime();
return (date.getTime() < today && yesterday <= date.getTime());
};
isToday = (date = new Date()) => {
return (new Date().toDateString() === date.toDateString());
};
isToYear = (date = new Date()) => {
return (new Date().getYear() === date.getYear());
};
getRecentDate = (date = new Date()) => {
if (isToday(date)) {
let temp = (new Date().getTime() - date.getTime) / 1000 / 60;
return temp <= 59 ? `${Math.ceil(temp))}分钟前` : dateFmt("HH:mm", date);
} else if (isYesterday(date)) {
return `昨天${dateFmt("HH:mm", date)}`;
} else if (isToYear(date)) {
return dateFmt("MM-dd HH:mm", date);
} else {
return dateFmt("yyyy-MM-dd HH:mm", date)
}
};
export default {
isYesterday,
isToday,
isToYear,
dateFmt,
getRecentDate,
getTimestamp,
}