js獲取指定小時日期格式化

不得不感嘆一下,聰明的程序員寫的代碼真是讓人驚奇javascript

找了一圈格式化代碼的方式,下面的這個使用了一個 slice 函數,真是厲害java

https://stackoverflow.com/questions/42358990/format-date-received-in-milliseconds-in-yyyy-mm-dd-hh-mm-in-jquery-javascriptjquery

不過裏面的一行代碼解決,但沒有徹底格式化爲 yyyyMMddHHmmss 的格式本身作了一些修改:程序員

    //獲取明天指定小時時間
    function getTomorrowtimeFormat (hour){
        var mydate = new Date();    
        mydate.setHours(hour);
        mydate.setDate(mydate.getDate()+1);
       
        var year = mydate.getFullYear();
        var month = mydate.getMonth()+1; 
        var day = mydate.getDate(); 
        var hour = mydate.getHours();
        var minutes = mydate.getMinutes();
        var seconds = mydate.getSeconds();
        var timestr = year + '' +
                    ('0' + month).slice(-2)+ '' +
                    ('0' + day).slice(-2)+ '' +
                    ('0' + hour).slice(-2)+ '' +
                    ('0' + minutes).slice(-2)+ ''+
                    ('0' + seconds).slice(-2);

        return timestr;
    }
    //獲取今天指定小時時間
    function getTodaytimeFormat (hour){
        var mydate = new Date();    
        mydate.setHours(hour);

        var year = mydate.getFullYear();    
        var month = mydate.getMonth()+1; 
        var day = mydate.getDate(); 
        var hour = mydate.getHours();
        var minutes = mydate.getMinutes();
        var seconds = mydate.getSeconds();
        var timestr = year + '' +
                    ('0' + month).slice(-2)+ '' +
                    ('0' + day).slice(-2)+ '' +
                    ('0' + hour).slice(-2)+ '' +
                    ('0' + minutes).slice(-2)+ ''+
                    ('0' + seconds).slice(-2);
        return timestr;
    }
相關文章
相關標籤/搜索