zoukankan      html  css  js  c++  java
  • js获取发布时间

    需求: 今天发布的消息则返回“今天 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&&currentTimeDay==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();
                    }
                }
            }
    
  • 相关阅读:
    ASP.NET初识4
    属性
    ASP.NET初识4
    ACCP6.0第九章练习
    ASP.NET初识1
    鼠标指针含义
    ASP.NET初识2
    第三部分
    ASP.NET初识3
    ASP.NET初识5
  • 原文地址:https://www.cnblogs.com/lyzz1314/p/14455252.html
Copyright © 2011-2022 走看看