时间戳处理
把时间戳处理成 标准时间
要求:此处理是按照 当日时间 只显示 小时 分钟 昨天的时间 处理成 昨天 + 小时 分钟
如果不跨年年 月 日 跨年显示 年 月 日
/***
- 时间格式的处理
*/
function getTimer(timer) {
let nowe = new Date(); // 当前的系统时间
let oldtime = new Date(timer); //目标时间
let t_day = oldtime.getDate();
let t_year = oldtime.getFullYear();
let t_month = oldtime.getMonth() + 1;
let t_hour = oldtime.getHours();
let t_minute = oldtime.getMinutes();
if (t_hour<10){
t_hour = '0' + t_hour
}
if (t_minute < 10) {
t_minute = '0' + t_minute
}
let n_year = nowe.getFullYear();
let n_month = nowe.getMonth() + 1;
let n_day = nowe.getDate();
let mistiming = nowe - oldtime; // 计算出当前时间,与目标时间的时间差
if (t_year == n_year) {
if (n_month == t_month) {
// 在同一月
if (n_day - t_day == 1) {
// 差等于1 昨天 + 时分
return '昨天' + ' ' + t_hour + ':' + t_minute
} else if (n_day - t_day == 0) {
// 显示 时分
return t_hour + ':' + t_minute
} else {
return t_month + '.' + t_day
}
}
} else {
//跨年了 显示 年 月 日
return t_year + '.' + t_month + '.' + t_day
}
}