需求: 今天发布的消息则返回“今天 12:12”,昨天的话则返回“昨天 20:20”,其他日期则返回3-13或2020-3-13(去年的情况下)
代码:
getPublishTime:()=> (time,full)=>{
let currentTime = new Date();
time = new Date(time);
let currentTimeDay = currentTime.getDate();
let timeDay = time.getDate();
let currentTimeYear = currentTime.getFullYear();
let timeYear = time.getFullYear();
if(currentTimeYear==timeYear&¤tTimeDay==timeDay){
return "今天 " + time.getHours() + ":" + (time.getMinutes()<10? '0'+time.getMinutes():time.getMinutes());
}else if(currentTimeYear==timeYear&&(currentTimeDay-1)==timeDay){
return "昨天 " + time.getHours() + ":" + (time.getMinutes()<10? '0'+time.getMinutes():time.getMinutes());
}else if(currentTimeYear==timeYear&&(currentTimeDay-timeDay)>1){
if(full){
return (time.getMonth()+1)+'-'+time.getDate()+' '+time.getHours() + ":" + (time.getMinutes()<10? '0'+time.getMinutes():time.getMinutes());
}else{
return (time.getMonth()+1)+'-'+time.getDate();
}
}else if(currentTimeYear>timeYear){
if(full){
return time.getFullYear()+' - '+(time.getMonth()+1)+' - '+time.getDate()+' '+time.getHours() + ":" + (time.getMinutes()<10? '0'+time.getMinutes():time.getMinutes());
}else{
return time.getFullYear()+' - '+(time.getMonth()+1)+' - '+time.getDate();
}
}
}