1. 程式人生 > >javascript時間日期處理--new Date(),format()

javascript時間日期處理--new Date(),format()

建立時間物件:

      var objDate=new Date([arguments list]); 
 

引數有5種形式:

     1)new Date("month dd,yyyy hh:mm:ss"); 
     2)new Date("month dd,yyyy"); 
     3)new Date(yyyy,mth,dd,hh,mm,ss); 

     4)new Date(yyyy,mth,dd); 
     5)new Date(ms); 

      6)new Date()//無引數為當前系統時間

對應:

    1)new Date("January 12,2006 22:19:35"); 或new Date("11 12,2006 22:19:35"); //注意字串裡的月為1-12
 
   2) new Date("January 12,2006"); 或new Date("11 12,2006");//注意字串裡的月為1-12
 
  3) new Date(2006,0,12,22,19,35); //數字時,這裡的月為0-12
 
  4) new Date(2006,0,12); //數字時,這裡的月為0-12
 
  5)new Date(1137075575000); //這時毫秒數,建立的時間和 GMT時間1970年1月1日之間相差的毫秒數

 

new Date(2020,02,0).getDate()/new Date(2020,2,0).getDate()--//返回29即返回2月的天數,第三個引數為0時,月為1-12,

經驗提醒:

遇到(遇到日期的加減時候,需要用時間戳,這樣可以規避跨年跨月平年潤年的問題,直接24*3600*100*天數 轉成日期格式即可)  

javascript時間戳和日期字串相互轉換

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">

// 1.獲取當前時間戳(以s為單位)  Date.parse(new Date());

     var timestamp = Date.parse(new Date());

          timestamp = timestamp / 1000;

console.log("當前時間戳為:" + timestamp)//當前時間戳為:1403149534

 

// 2.獲取某個時間格式的時間戳  Date.parse(new Date("yyyy-MM-dd hh:mm:ss"))

var stringTime = "2014-07-10 10:21:12";

var timestamp2 = Date.parse(new Date(stringTime));

timestamp2 = timestamp2 / 1000;

//2014-07-10 10:21:12的時間戳為:1404958872 

console.log(stringTime + "的時間戳為:" + timestamp2);

 

// 3.將當前時間換成時間格式字串

var timestamp3 = 1403058804;//單位s

var newDate = new Date();

newDate.setTime(timestamp3 * 1000);//設定實際時間

console.log(newDate.toDateString());// "Wed Jun 18 2014" (將實際時間轉為字串)

console.log(newDate.toGMTString());// "Wed, 18 Jun 2014 02:33:24 GMT "(將實際時間轉為GMT字串)

console.log(newDate.toISOString());//"2014-06-18T02:33:24.000Z"(將實際時間轉為ISO字串)

console.log(newDate.toJSON());//"2014-06-18T02:33:24.000Z "(將實際時間轉為JSON字串)

console.log(newDate.toLocaleDateString());//"2014/6/18"(將實際時間轉為LocaleDate字串)

console.log(newDate.toLocaleString());//"2014/6/18  上午10:33:24"(將實際時間轉為Locale字串)

console.log(newDate.toLocaleTimeString());//"上午10:33:24 "

console.log(newDate.toString());//"Wed Jun 18 2014 10:33:24 GMT+0800 (中國標準時間)"

console.log(newDate.toTimeString());//"10:33:24 GMT+0800 (中國標準時間) "

console.log(newDate.toUTCString());//"Wed, 18 Jun 2014 02:33:24 GMT"

------全域性格式化輸出配置 年月日

Date.prototype.format = function(format) {

      var date = {

              "M+": this.getMonth() + 1,

              "d+": this.getDate(),

              "h+": this.getHours(),

              "m+": this.getMinutes(),

              "s+": this.getSeconds(),

              "q+": Math.floor((this.getMonth() + 3) / 3),

              "S+": this.getMilliseconds()

      };

      if (/(y+)/i.test(format)) {

              format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));

      }

      for (var k in date) {

              if (new RegExp("(" + k + ")").test(format)) {

                    format = format.replace(RegExp.$1, RegExp.$1.length == 1

                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));

              }

      }

      return format;

}

console.log(newDate.format('yyyy-MM-dd h:m:s'));

</script>