js 日期函數用法總結

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   使用   ar   strong   

1 建立Date對象,用於處理日期和時間

var date=new Date();Date對象會把當前日期和時間儲存為初始值。還可以設定其它參數初始化 Date對象:new Date("month dd,yyyy hh:mm:ss");new Date("month dd,yyyy");new Date(yyyy,mth,dd,hh,mm,ss);new Date(yyyy,mth,dd);new Date(ms);new Date("yyyy/MM/dd");參數說明:month:用英文表示月份名稱,從January到Decembermth:用整數表示月份,從0-11(1月到12月)dd:表示一個月中的第幾天,從1到31yyyy:四位元表示的年份hh:小時數,從0(午夜)到23(晚11點)mm:分鐘數,從0到59的整數ss:秒數,從0到59的整數ms:毫秒數,為大於等於0的整數如:new Date("January 12,2006 22:19:35");new Date("January 12,2006");new Date(2006,0,12,22,19,35);new Date(2006,0,12);new Date(1137075575000);new Date("2014/09/22");

 

2. Date 對象的方法

              日期存取方法:

Method(方法)

Description (描述)

Value(數值)

getYear()

返回年份的最後兩位元字

2001

getMonth()

返回年份中的第幾月(0到11)

5

getDate()

返回月份中的第幾日(1到31)

2

getDay()

返回星期中的第幾天 (0到6)

6

getTimezoneOffset()

返回當地時間與格林尼治天文台平均時間的差別

-480 (-8h)

getHours()

返回一天中的時針點數(0到23)

16

getMinutes()

返回分鐘 (0..59)

8

getSeconds()

返回時間的秒 (0到59)

24

getTime()

返回自從公元1970年1月1日的毫秒數

991469304470

日期設定方法

setDate()

設定每月中的第幾天(從0到30)

setHours()

設定小時(從0到23)

setMinutes()

設定分鐘(從0到59)

setMonth()

設定月份(從0到11)

setSeconds()

設定秒數(從0到59)

setTime()

設定時間(從公元以來的毫秒數)

setYear()

設定年份

其它的日期方法:

parse

轉化日期文字為自從公元以來的毫秒數,比如Date.parse(“date string”)

toString()

Sat Jun 2 16:08:24 UTC+0800 2001

toGMTString()

Sat, 2 Jun 2001 08:08:24 UTC

toLocaleString()

2001年6月2日 16:08:24

 

應用例子:使用getDay()方法擷取星期數

var myDate=new Date();var weekCn=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];alert(weekCn[myDate.getDay()]);

 

3  通過擴充方法format格式化 Date

// 對Date的擴充,將 Date 轉化為指定格式的String// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個預留位置, // 年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz     var o = {        "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+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));    for (var k in o)    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));    return fmt;}

調用:
new Date().format(‘yyyy-MM-dd‘);

 

其它實用方法:

//函數名:CheckDateTime      //功能介紹:檢查是否為日期  function  CheckDate(str){                                   var  reg  =  /^(\d+)-(\d{1,2})-(\d{1,2})$/;           var  r  =  str.match(reg);           if(r==null)return  false;           r[2]=r[2]-1;           var  d=  new  Date(r[1],  r[2],r[3]);           if(d.getFullYear()!=r[1])return  false;           if(d.getMonth()!=r[2])return  false;           if(d.getDate()!=r[3])return  false;             return  true;  }

調用:
alert(CheckDate("2014-9-23"))  

 

js 日期函數用法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.