获取当前时间方法

function getDate(format){
    //获得当前系统的时间
    var myDate = new Date();
    var year = myDate.getYear()+"";                                 //获取当前年份
    var fullyear = myDate.getFullYear().toString();                            //获取完整的年份(4位,1970) 
    var Month = myDate.getMonth()+1;                                //获取当前月份(0-11,0代表1月)
    var NowDate = myDate.getDate();                                 //获取当前日(1-31)
    var hours = myDate.getHours();                                  //获取当前小时数(0-23)
    var minutes = myDate.getMinutes();                              //获取当前分钟数(0-59)
    var seconds = myDate.getSeconds();                              //获得当前的分秒数
    var getMilisecond = myDate.getMilliseconds();                   //获得当前的毫秒数(0-999)
    if(Month<10){  Month = "0"+Month;  }
    if(NowDate<10){    NowDate = "0"+NowDate;  }
    if(hours<10){  hours = "0"+hours;  }
    if(minutes<10){ minutes = "0"+minutes;   }
    if(seconds<10){    seconds = "0"+seconds;  }
    //获取日期与时间
    if (format == "YY"){
        if(fullyear&&fullyear.length==4){
            return fullyear.substring(2);
        }else{
            return "11";
        }
    }
    if (format == "YYYY"){
        if(fullyear&&fullyear.length==4){
            return fullyear;
        }else{
            return "2011";
        }
    }
    if (format == "YYYY-MM-DD"){       return fullyear+"-"+Month+"-"+NowDate;   }
    if (format == "HH:MI:SS"){   return hours+":"+minutes+":"+seconds ; }
    if (format == "YYYY-MM-DD HH:MI:SS"){  
        return fullyear+"-"+Month+"-"+NowDate +" "+ hours+":"+minutes+":"+seconds 
    }
}
posted on 2012-02-19 11:09  JimmyCai  阅读(267)  评论(0编辑  收藏  举报