js获取当前时间

/**
     * getNowFormatDate 获取当前时间
     * @param msec 毫秒值
     */
    getNowFormatDate :function(msecVal){
        if(msecVal){
            var now = new Date(msecVal); //毫秒值转为完整时间
        }else{
            var now = new Date(); //完整时间
        };
        var month = now.getMonth() + 1; //当前月份
        var day = now.getDate();        //当前天
        var Hours = now.getHours();     //当前时
        var Minutes = now.getMinutes(); //当前分
        var Seconds = now.getSeconds(); //当前秒
        if(month >= 1 && month <= 9){   //
            month = "0" + month;
        };
        if(day >= 0 && day <= 9){ //
            day = "0" + day;
        };
        if(Hours >= 0 && Hours <= 9){ //
            Hours = "0" + Hours;
        };
        if(Minutes >= 0 && Minutes <= 9){ //
            Minutes = "0" + Minutes;
        };
        if(Seconds >= 0 && Seconds <= 9){ //
            Seconds = "0" + Seconds;
        };
        var currentdate = now.getFullYear() + "-" + month + "-" + day + " " + 
            Hours + ":" + Minutes + ":" + Seconds;
        return currentdate;
    },

猜你喜欢

转载自www.cnblogs.com/hjbky/p/9102363.html