javascript時間函數

來源:互聯網
上載者:User

javascript時間函數 
javascript提供了Date對象來進行時間和日期的計算。

Date對象有多種建構函式:

new Date() //目前時間
new Date(milliseconds) //距離起始時間1970年1月1日的毫秒數
new Date(datestring) //字串代表的日期與時間。此字串可以使用Date.parse()轉換,比如"Jannuary 1, 1998 20:13:15"
new Date(year, month, day, hours, minutes, seconds, microseconds) //時間數值,可以不用全部寫,不寫則預設為0

使用時先建立一個Date對象,比如:
dateObj=new Date();

然後調用函數,比如
year=dateObj.getFullYear();//獲得年份值

下面是函數列表。

擷取類函數:
getDate() 函數 -- 返回天數(1-31)
getDay()函數 -- 返回星期數(0-6)
getFullYear() 函數 -- 返回四位元年份
getHours()函數 -- 返回小時數(0-23)
getMilliseconds() 函數 -- 返回毫秒數(0-999)
getMinutes() 函數 -- 返回分鐘數(0-59)
getMonth() 函數 -- 返回月份數(0-11)
getSeconds() 函數 -- 返回的秒數(0-59)
getTime() 函數 -- 返回時間戳記標記法(毫秒錶示)
getYear() 函數 -- 返回年份(真實年份減去1900)

設定類函數:
(以下函數均返回date對象距1970年1月1日午夜之間的毫秒數)
setDate() 函數 -- 設定月份的一天
setFullYear() 函數 -- 設定的年份,月份和天
setHours() 函數 -- 設定小時,分鐘,秒和毫秒
setMilliseconds() 函數 -- 設定毫秒數
setMinutes() 函數 -- 設定分鐘,秒,毫秒
setMonth() 函數 -- 設定月份,天
setSeconds() 函數 -- 設定月份的一天
setTime() 函數 -- 使用毫秒數設定date對象
setYear() 函數 -- 設定年份(真實年份減去1900)

轉化顯示類函數:
toLocalString() 函數 -- 返回本地化字串表示
toLocaleDateString函數 -- 返回日期部分的本地化字串
toLocaleTimeString函數 -- 返回時間部分的本地化字串

相對於local輸出,還有:

toString()
toDateString()
toTimeString()

區別在於前者是根據不同的機器有不同的當地語言格式,後者是內部表示格式

日期解析類函數
Date.parse() 函數 -- 解析一個日期的字串,並返回該日期距1970年1月1日午夜之間的毫秒數

時區相關的部份請看javascript時區函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.