仿微信发消息时间显示,刚刚,昨天,星期几

显示规则:
今日 HH:mm
昨天 昨天 HH:mm
一周内 星期几 HH:mm(从6天前的0点开始算)
一周前 yy/MM/dd HH:mm

参考文章:https://segmentfault.com/a/1190000014795596?utm_source=tag-newest

代码如下:

        function showTime(display){

            let nowTime = new Date();//当前时间
            nowYear = nowTime.getFullYear();
            nowMonth = nowTime.getMonth() + 1;
            nowDay = nowTime.getDate();
            nowHours = nowTime.getHours();
            nowMinutes = nowTime.getMinutes();
        
            let displayTime = new Date(display);//参数时间
            displayYear = displayTime.getFullYear();
            displayMonth = displayTime.getMonth() + 1;
            displayDay = displayTime.getDate();
            displayHours = displayTime.getHours();
            displayMinutes = displayTime.getMinutes();

            const IN_Minutes = 3 * 60 * 1000;//3分钟
            const IN_DAY = 24 * 60 * 60 * 1000;//1天
        
            if(nowTime-displayTime <= IN_Minutes){
            
                return '刚刚'
            
            }else if(displayYear==nowYear && displayMonth == nowMonth &&  nowDay==displayDay){
            
                return zeroize(displayHours) + ":" + zeroize(displayMinutes)
            
            }else if(displayYear==nowYear && displayMonth == nowMonth &&  displayDay== nowDay-1){
                
                return '昨天' + zeroize(displayHours) + ":" + zeroize(displayMinutes)
            
            }else if(nowTime-displayTime

你可能感兴趣的:(仿微信发消息时间显示,刚刚,昨天,星期几)