zoukankan      html  css  js  c++  java
  • js通过某个时间戳获取昨天、今天、明天、后天的时刻

    function getTimeTransition(timestamp){
            let now_timestamp = (Date.parse(new Date())) / 1000;//现在时间戳
            let today_timestamp = getStartEndTime();//今天的起始时间戳
            let yesterday_timestamp = getStartEndTime(1);//昨天的起始时间戳
            let tomorrow_timestamp = getStartEndTime(-1);//明天的起始时间戳
            let acquired_timestamp = getStartEndTime(-2);//后天的起始时间戳
            if(today_timestamp[0] < timestamp && today_timestamp[1] > timestamp){
                //今日时间戳
                return "今日"+formatDate(timestamp,"h:i")
            }else{
                //非今日时间戳
                if((timestamp - now_timestamp) > 0){
                    //将来时间
                    console.log("将来时间")
                    if(tomorrow_timestamp[0] < timestamp && tomorrow_timestamp[1] > timestamp){
                        return "明天"+formatDate(timestamp,"h:i")
                    }else{
                        if(acquired_timestamp[0] < timestamp && acquired_timestamp[1] > timestamp){
                            return "后天"+formatDate(timestamp,"h:i")
                        }else{
                            let time_count = parseInt((timestamp - now_timestamp) / 86400);
                            if(time_count < 4){
                                return time_count + "天后"+formatDate(timestamp,"h:i");
                            }else{
                                return formatDate(timestamp,"m-d h:i")
                            }
                        }
                    }
                }else{
                    //过去时间
                    console.log("过去时间")
                    if(yesterday_timestamp[0] < timestamp && yesterday_timestamp[1] > timestamp){
                        return "昨天"+formatDate(timestamp,"h:i")
                    }else{
                        let time_count = parseInt((now_timestamp - timestamp) / 86400);
                        if(time_count < 4){
                            return  time_count + "天前"+formatDate(timestamp,"h:i");
                        }else{
                            return formatDate(timestamp,"m-d h:i")
                        }
                    }
                }
            }
            //获取某天的起止时间戳(今日传入0,昨日传入1,明日传入-1)
            function getStartEndTime (num = 0) {
                // 一天的毫秒数
                const MillisecondsADay = 24*60*60*1000 * num
                // 今日开始时间戳
                const todayStartTime = new Date(new Date().setHours(0, 0, 0, 0)).getTime()
                // 今日结束时间戳
                const todayEndTime = new Date(new Date().setHours(23,59,59,999)).getTime()
                // 昨日开始时间戳
                const yesterdayStartTime = todayStartTime - MillisecondsADay
                // 昨日结束时间戳
                const yesterdayEndTime = todayEndTime - MillisecondsADay
                return [
                    parseInt(yesterdayStartTime / 1000),
                    parseInt(yesterdayEndTime / 1000)
                ]
            }
            //时间戳转指定时间
            function formatDate(Unix,formate = 'Y-m-d h:i:s',complement = true,alike = false){
                let current = new Date(Date.parse(new Date()));
                let current_year = current.getFullYear();
                let current_month = current.getMonth() + 1;
                let current_date = current.getDate();
                let timestamp = new Date(Unix * 1000);
                let year = timestamp.getFullYear();
                let month = timestamp.getMonth() + 1;
                let date = timestamp.getDate();
                let hour = timestamp.getHours();
                let minute = timestamp.getMinutes();
                let second = timestamp.getSeconds();
                if(alike && current_year === year){
                    formate = formate.replace(/Y-/g,'');
                }else{
                }
                if(alike && current_year === year && current_month === month && current_date === date){
                    formate = formate.replace(/m-d /g,'');
                }
                if(complement){
                    month = month < 10 ? '0' + month : month;
                    date = date < 10 ? '0' + date : date;
                    hour = hour < 10 ? '0' + hour : hour;
                    minute = minute < 10 ? '0' + minute : minute;
                    second = second < 10 ? '0' + second : second;
                }
                formate = formate.replace(/Y/g,year);
                formate = formate.replace(/m/g,month);
                formate = formate.replace(/d/g,date);
                formate = formate.replace(/h/g,hour);
                formate = formate.replace(/i/g,minute);
                formate = formate.replace(/s/g,second);
                return formate;
            }
        }
        console.log(getTimeTransition(1635242400));
    

      

  • 相关阅读:
    JQuery中的id选择器含有特殊字符时,不能选中dom元素
    解决Mac下MySQL登录问题
    Mac 安装mysql
    禁止chrome浏览器自动填充表单的解决方案
    Eclipse 编译错误 Access restriction: The type 'JPEGCodec' is not API (restriction on required library 'C:Program FilesJavajre7lib t.jar')
    羊皮纸月亮计划
    ActionSupport.getText()方法
    linux入门经验之谈
    tomcat设置默认启动项
    网页设置下载apk
  • 原文地址:https://www.cnblogs.com/ffyun/p/15439043.html
Copyright © 2011-2022 走看看