js獲取當前日期時間


獲取當前日期

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }

調用直接獲取當天日期

獲取當前日期時間

    //獲取當前日期時間
    function curentTime()
    {
        var now = new Date();

        var year = now.getFullYear();       //
        var month = now.getMonth() + 1;     //
        var day = now.getDate();            //

        var hh = now.getHours();            //
        var mm = now.getMinutes();          //
        var ss = now.getSeconds();          //

        var clock = year + "-";

        if(month < 10)
            clock += "0";

        clock += month + "-";

        if(day < 10)
            clock += "0";

        clock += day + " ";

        if(hh < 10)
            clock += "0";

        clock += hh + ":";
        if (mm < 10)
            clock += '0';
        clock += mm + ":";

        if (ss < 10)
            clock += '0';
        clock += ss;
        return clock;
    }

    //定時執行setTime
    setInterval("setTime()",1000);

    //將id為currentTime的div更新為最新時間
    function setTime(){
        $('#currentTime').html(curentTime());
    }

根據年月獲取當月第一天和最后一天

        //根據年月獲取當月第一天日期
        function getStartDate(yearmonthstr){
            if(/^\d{4}-\d{2}$/.test(yearmonthstr)){ //判斷是否滿足yyyy-mm條件
                var year = /\d{4}/.exec(yearmonthstr)[0];   //獲取年份
                var month = /\d{2}$/.exec(yearmonthstr)[0];  //獲取月份
                var d = new Date(year, month, 0);
                 var day = d.getDate();  //獲取月的天數
                 return yearmonthstr+'-01';
             }
        }
        //根據年月獲取當月最后一天日期
        function getEndDate(yearmonthstr){
            if(/^\d{4}-\d{2}$/.test(yearmonthstr)){ //判斷是否滿足yyyy-mm條件
                var year = /\d{4}/.exec(yearmonthstr)[0];  //獲取年份
                var month = /\d{2}$/.exec(yearmonthstr)[0];  //獲取月份
                var d = new Date(year, month, 0);
                 var day = d.getDate();  //獲取月的天數
                 return yearmonthstr+'-'+day;
             }
        }

調用,參數為yyyy-MM.

 


免責聲明!

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



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