時間大小比較


1.整點時間大小比較 例:08:20:21  和 08:22:23的大小

 

let z = '20:01:00';
let z1 = '10:00:00';
let date = new Date();
console.log(date) //Fri Jul 17 2020 09:54:52 GMT+0800 (中國標准時間)
let a = z.split(":");
let b = z1.split(":");
console.log(a,b)//["20", "01", "00"] (3) ["10", "00", "00"]
console.log(date.setHours(a[0], a[1],a[2]),date.setHours(b[0], b[1]),b[2])

 

  

 

2.日期時間大小比較

 

var startDate1 ='2012-12-30 00:00:00' ;
let start1 = new Date(startDate1.replace(/\-/g, "\/"));
 
var startDate2 ='2012-12-30 13:13:02' ;
let start2 = new Date(startDate2.replace(/\-/g, "\/"));
start2 .getTime();// 獲取時間戳 單位毫秒
 
console.log(start1,start1<start2)          //Sun Dec 30 2012 00:00:00 GMT+0800 (中國標准時間) true

 

  

 

3.當前日期加一天

<script type="text/javascript">
    function addDay(datetime, days) {
        var old_time = new Date(datetime.replace(/-/g, "/")); //替換字符,js不認2013-01-31,只認2013/01/31
        var fd = new Date(old_time.valueOf() + days * 24 * 60 * 60 * 1000); //日期加上指定的天數
        var new_time = fd.getFullYear() + "-";
        var month = fd.getMonth() + 1;
        if (month >= 10) {
            new_time += month + "-";
        } else {
            //在小於10的月份上補0
            new_time += "0" + month + "-";
        }
        if (fd.getDate() >= 10) {
            new_time += fd.getDate();
        } else {
            //在小於10的日期上補0
            new_time += "0" + fd.getDate();
        }
        return new_time; //輸出格式:2013-01-02
    }

    alert(addDay("2013-11-12", 1)); //彈出值:2013-11-13
</script>

  

4.獲取當天

 

          var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth(); //得到月份
            var date = now.getDate(); //得到日期
            var day = now.getDay(); //得到周幾
            var hour = now.getHours(); //得到小時
            var minu = now.getMinutes(); //得到分鍾
            var sec = now.getSeconds(); //得到秒
            month = month + 1;
            if (month < 10) month = "0" + month;
            if (date < 10) date = "0" + date;
            if (hour < 10) hour = "0" + hour;
            if (minu < 10) minu = "0" + minu;
            if (sec < 10) sec = "0" + sec;
            var time = "";
            time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM