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