關於new Date()的日期格式處理

new Date()基本方法:

var myDate = new Date();  
myDate.getYear(); //獲取當前年份(2位)  
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)  
myDate.getMonth(); //獲取當前月份(0-11,0表明1月)         // 因此獲取當前月份是myDate.getMonth()+1;   
myDate.getDate(); //獲取當前日(1-31)  
myDate.getDay(); //獲取當前星期X(0-6,0表明星期天)  
myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)  
myDate.getHours(); //獲取當前小時數(0-23)  
myDate.getMinutes(); //獲取當前分鐘數(0-59)  
myDate.getSeconds(); //獲取當前秒數(0-59)  
myDate.getMilliseconds(); //獲取當前毫秒數(0-999)  
myDate.toLocaleDateString(); //獲取當前日期  
var mytime=myDate.toLocaleTimeString(); //獲取當前時間  
myDate.toLocaleString( ); //獲取日期與時間  

將字符串形式的日期轉換成日期對象

var date= new Date(Date.parse(strTime.replace(/-/g,  "/")));//轉換成Data();

日期格式轉爲日期標準字符串:2015-03-19(一)

var formatDate = function (date) {  
    var y = date.getFullYear();  
    var m = date.getMonth() + 1;  
    m = m < 10 ? '0' + m : m;  
    var d = date.getDate();  
    d = d < 10 ? ('0' + d) : d;  
    return y + '-' + m + '-' + d;  
};  

日期格式轉爲日期標準字符串:2015-03-19(二)

arr.date = new Date();
      var d = new Date(arr.date);
      var yue = (d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
      var re = d.getDate() > 9 ? d.getDate() : '0' + d.getDate();
      var youWant = d.getFullYear() + '-' + yue + '-' + re;
      arr.date = youWant;

(返回的就是轉換以後的格式~~)spa

時間戳轉爲日期格式

//時間戳轉日期格式  
      var formatDateTime3 = function(time, format){  
          var t = new Date(time);  
          var tf = function(i){return (i < 10 ? '0' : '') + i};  
          return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){  
              switch(a){  
                  case 'yyyy':  
                      return tf(t.getFullYear());  
                      break;  
                  case 'MM':  
                      return tf(t.getMonth() + 1);  
                      break;  
                  case 'mm':  
                      return tf(t.getMinutes());  
                      break;  
                  case 'dd':  
                      return tf(t.getDate());  
                      break;  
                  case 'HH':  
                      return tf(t.getHours());  
                      break;  
                  case 'ss':  
                      return tf(t.getSeconds());  
                      break;  
              }  
          })  
      }; 

時間格式字符串轉爲時間戳(毫秒)

var time1=‘2016-01-01 17:22:37’;  
var date=new Date(time1.replace(/-/g, '/'));  //開始時間  
var time2=date.getTime();  
相關文章
相關標籤/搜索