js获取当前时间(yyyy-MM-dd HH:mm:ss)

js 获取当前时间,格式为yyyy-MM-dd HH:mm:ss

function getCurrentDate() {

   var date = new Date();
   var format1 = "-";
   var format2 = ":";
   var month = date.getMonth() + 1;//date.getMonth() 的值为0到11
   var day = date.getDate();//日
   var hours = date.getHours();//时
   var minutes = date.getMinutes();//分
   var seconds = date.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 = date.getFullYear() + format21 + month + seperator1 + day
           + " " + hours + seperator2 + minutes
           + format2 + seconds;


   return currentdate;
}

你可能感兴趣的:(前端)