<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 概述 Date對象是JavaScript提供的日期和時間的操作接口。它有多種用法。 ### Date() 作為一個函數,Date對象可以直接調用,返回一個當前日期和時間的字符串。 ~~~ Date() // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" Date(2000, 1, 1) // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" ~~~ 上面代碼說明,無論有沒有參數,直接調用Date總是返回當前時間。 ### new Date() Date對象還是一個構造函數,對它使用new命令,會返回一個Date對象的實例。如果不加參數,生成的就是代表當前時間的對象。 ~~~ var today = new Date(); today // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" // 等同于 today.toString() // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" ~~~ ### new Date(milliseconds) Date對象接受從1970年1月1日00:00:00 UTC開始計算的毫秒數作為參數。這意味著如果將Unix時間戳作為參數,必須將Unix時間戳乘以1000。 ~~~ new Date(1378218728000) // Tue Sep 03 2013 22:32:08 GMT+0800 (CST) // 1970年1月2日的零時 var Jan02_1970 = new Date(3600*24*1000); // Fri Jan 02 1970 08:00:00 GMT+0800 (CST) // 1969年12月31日的零時 var Dec31_1969 = new Date(-3600*24*1000); // Wed Dec 31 1969 08:00:00 GMT+0800 (CST) ~~~ 上面代碼說明,Date構造函數的參數可以是一個負數,表示1970年1月1日之前的時間。Date對象能夠表示的日期范圍是1970年1月1日前后各一億天。 ### new Date(datestring) Date對象還接受一個日期字符串作為參數,返回所對應的時間。 ~~~ new Date("January 6, 2013"); // Sun Jan 06 2013 00:00:00 GMT+0800 (CST) ~~~ 所有可以被Date.parse()方法解析的日期字符串,都可以當作Date對象的參數。 ~~~ new Date("2013-02-15") new Date("2013-FEB-15") new Date("FEB, 15, 2013") new Date("FEB 15, 2013") new Date("Feberuary, 15, 2013") new Date("Feberuary 15, 2013") new Date("15, Feberuary, 2013") // Fri Feb 15 2013 08:00:00 GMT+0800 (CST) ~~~ 上面多種寫法,返回的都是同一個時間。 ### new Date(year, month [, day, hours, minutes, seconds, ms]) 在多個參數的情況下,Date對象將其分別視作對應的年、月、日、小時、分鐘、秒和毫秒。如果采用這種用法,最少需要指定兩個參數(年和月),其他參數都是可選的,默認等于0。如果只使用年一個參數,Date對象會將其解釋為毫秒數。 ~~~ new Date(2013) // Thu Jan 01 1970 08:00:02 GMT+0800 (CST) new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) new Date(2013,0,1,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) new Date(2013,0,1,0,0,0,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) ~~~ 上面代碼(除了第一行)返回的是2013年1月1日零點的時間,可以看到月份從0開始計算,因此1月是0,12月是11。但是,月份里面的天數從1開始計算。 這些參數如果超出了正常范圍,會被自動折算。比如,如果月設為15,就算折算為下一年的4月。參數還可以使用負數,表示扣去的時間。 ~~~ new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) new Date(2013,-1) // Sat Dec 01 2012 00:00:00 GMT+0800 (CST) new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (CST) new Date(2013,0,0) // Mon Dec 31 2012 00:00:00 GMT+0800 (CST) new Date(2013,0,-1) // Sun Dec 30 2012 00:00:00 GMT+0800 (CST) ~~~ 上面代碼分別對月和日使用了負數,表示從基準日扣去相應的時間。 年的情況有所不同,如果為0,表示1900年;如果為負數,則表示公元前。 ~~~ new Date(1,0) // Tue Jan 01 1901 00:00:00 GMT+0800 (CST) new Date(0,0) // Mon Jan 01 1900 00:00:00 GMT+0800 (CST) new Date(-1,0) // Fri Jan 01 -1 00:00:00 GMT+0800 (CST) ~~~ ### 日期的運算 類型轉換時,Date對象的實例如果轉為數值,則等于對應的毫秒數;如果轉為字符串,則等于對應的日期字符串。所以,兩個日期對象進行減法運算,返回的就是它們間隔的毫秒數;進行加法運算,返回的就是連接后的兩個字符串。 ~~~ var then = new Date(2013,2,1); var now = new Date(2013,3,1); now - then // 2678400000 now + then // "Mon Apr 01 2013 00:00:00 GMT+0800 (CST)Fri Mar 01 2013 00:00:00 GMT+0800 (CST)" ~~~ ## Date對象的方法 ### Date.now() now方法返回當前距離1970年1月1日 00:00:00 UTC的毫秒數(Unix時間戳乘以1000)。 ~~~ Date.now() // 1364026285194 ~~~ 如果需要更精確的時間,可以使用window.performance.now()。它提供頁面加載到命令運行時的已經過去的時間,單位是浮點數形式的毫秒。 ~~~ window.performance.now() // 21311140.415 ~~~ ### Date.parse() parse方法用來解析日期字符串,返回距離1970年1月1日 00:00:00的毫秒數。日期字符串的格式應該完全或者部分符合RFC 2822和ISO 8061,即YYYY-MM-DDTHH:mm:ss.sssZ格式,其中最后的Z表示時區,是可選的。 * YYYY * YYYY-MM * YYYY-MM-DD * THH:mm(比如“T12:00”) * THH:mm:ss * THH:mm:ss.sss 請看例子。 ~~~ Date.parse("Aug 9, 1995") // 返回807897600000,以下省略返回值 Date.parse("January 26, 2011 13:51:50") Date.parse("Mon, 25 Dec 1995 13:30:00 GMT") Date.parse("Mon, 25 Dec 1995 13:30:00 +0430") Date.parse("2011-10-10") Date.parse("2011-10-10T14:48:00") ~~~ 如果解析失敗,返回NaN。 ~~~ Date.parse("xxx") // NaN ~~~ ### Date.UTC() 默認情況下,Date對象返回的都是當前時區的時間。Date.UTC方法可以返回UTC時間(世界標準時間)。該方法接受年、月、日等變量作為參數,返回當前距離1970年1月1日 00:00:00 UTC的毫秒數。 ~~~ // 使用的格式 Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]]) Date.UTC(2011,0,1,2,3,4,567) // 1293847384567 ~~~ 該方法的參數用法與Date構造函數完全一致,比如月從0開始計算。 ## Date實例對象的方法 使用new命令生成的Date對象的實例,有很多自己的方法。 ### Date.prototype.toString() toString方法返回一個完整的時間字符串。 ~~~ var today = new Date(1362790014000); today.toString() // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" today // "Sat Mar 09 2013 08:46:54 GMT+0800 (CST)" ~~~ 因為toString是默認的調用方法,所以如果直接讀取Date對象實例,就相當于調用這個方法。 ### Date.prototype.toUTCString(),Date.prototype.toISOString() toUTCString方法返回對應的UTC時間,也就是比北京時間晚8個小時。toISOString方法返回對應時間的ISO8601寫法。 ~~~ var today = new Date(1362790014000); today.toUTCString() // "Sat, 09 Mar 2013 00:46:54 GMT" today.toISOString() // "2013-03-09T00:46:54.000Z" ~~~ ### Date.prototype.toDateString(),Date.prototype.toTimeString() toDateString方法返回日期的字符串形式,toTimeString方法返回時間的字符串形式。 ~~~ var today = new Date(1362790014000); today.toDateString() // "Sat Mar 09 2013" today.toTimeString() // "08:46:54 GMT+0800 (CST)" ~~~ ### Date.prototype.toLocalDateString(),Date.prototype.toLocalTimeString() toLocalDateString方法返回一個字符串,代表日期的當地寫法;toLocalTimeString方法返回一個字符串,代表時間的當地寫法。 ~~~ var today = new Date(1362790014000); today.toLocaleDateString() // "2013年3月9日" today.toLocaleTimeString() "上午8:46:54" ~~~ ### Date.prototype.valueOf() valueOf方法返回實例對象距離1970年1月1日00:00:00 UTC對應的毫秒數,該方法等同于getTime方法。 ~~~ var today = new Date(); today.valueOf() // 1362790014817 today.getTime() // 1362790014817 ~~~ 該方法可以用于計算精確時間。 ~~~ var start = new Date(); doSomething(); var end = new Date(); var elapsed = end.getTime() - start.getTime(); ~~~ ### Date.prototype.get系列方法 Date對象提供了一系列get方法,用來獲取實例對象某個方面的值。 * Date.prototype.getTime():返回實例對象距離1970年1月1日00:00:00對應的毫秒數,等同于valueOf方法。 * Date.prototype.getDate():返回實例對象對應每個月的幾號(從1開始)。 * Date.prototype.getDay():返回星期,星期日為0,星期一為1,以此類推。 * Date.prototype.getFullYear():返回四位的年份。 * Date.prototype.getMonth():返回月份(0表示1月,11表示12月)。 * Date.prototype.getHours():返回小時(0-23)。 * Date.prototype.getMilliseconds():返回毫秒(0-999)。 * Date.prototype.getMinutes():返回分鐘(0-59)。 * Date.prototype.getSeconds():返回秒(0-59)。 * Date.prototype.getTimezoneOffset():返回當前時間與UTC的時區差異,以分鐘表示,返回結果考慮到了夏令時因素。 ~~~ var d = new Date("January 6, 2013"); d.getDate() // 6 d.getMonth() // 0 d.getFullYear() // 2013 d.getTimezoneOffset() // -480 ~~~ 上面這些方法默認返回的都是當前時區的時間,Date對象還提供了這些方法對應的UTC版本,用來返回UTC時間,比如getUTCFullYear()、getUTCMonth()、getUTCDay()、getUTCHours()等等。 ### Date.prototype.set系列方法 Date對象提供了一系列set方法,用來設置實例對象的各個方面。 * Date.prototype.setDate(date):設置實例對象對應的每個月的幾號(1-31),返回改變后毫秒時間戳。 * Date.prototype.setFullYear(year [, month, date]):設置四位年份。 * Date.prototype.setHours(hour [, min, sec, ms]):設置小時(0-23)。 * Date.prototype.setMilliseconds():設置毫秒(0-999)。 * Date.prototype.setMinutes(min [, sec, ms]):設置分鐘(0-59)。 * Date.prototype.setMonth(month [, date]):設置月份(0-11)。 * Date.prototype.setSeconds(sec [, ms]):設置秒(0-59)。 * Date.prototype.setTime(milliseconds):設置毫秒時間戳。 ~~~ var d = new Date ("January 6, 2013"); d // Sun Jan 06 2013 00:00:00 GMT+0800 (CST) d.setDate(9) // 1357660800000 d // Wed Jan 09 2013 00:00:00 GMT+0800 (CST) ~~~ set方法的參數都會自動折算。以setDate為例,如果參數超過當月的最大天數,則向下一個月順延,如果參數是負數,表示從上個月的最后一天開始減去的天數。 ~~~ var d = new Date("January 6, 2013"); d.setDate(32) // 1359648000000 d // Fri Feb 01 2013 00:00:00 GMT+0800 (CST) var d = new Date ("January 6, 2013"); d.setDate(-1) // 1356796800000 d // Sun Dec 30 2012 00:00:00 GMT+0800 (CST) ~~~ 使用setDate方法,可以算出今天過后1000天是幾月幾日。 ~~~ var d = new Date(); d.setDate( d.getDate() + 1000 ); d.getDay(); ~~~ set系列方法除了setTime(),都有對應的UTC版本,比如setUTCHours()。 ### Date.prototype.toJSON() toJSON方法返回JSON格式的日期對象。 ~~~ var jsonDate = (new Date()).toJSON(); jsonDate "2013-09-03T14:26:31.880Z" var backToDate = new Date(jsonDate); ~~~ ## 參考鏈接 * Rakhitha Nimesh,[Getting Started with the Date Object](http://jspro.com/raw-javascript/beginners-guide-to-javascript-date-and-time/) * Ilya Kantor,?[Date/Time functions](http://javascript.info/tutorial/datetime-functions)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看