淺談JS日期(Date)處理函數(shù)
獲取日期
1、Date()
——返回當(dāng)日的日期和時(shí)間。
2、getDate()
——從 Date 對(duì)象返回一個(gè)月中的某一天 (1 ~ 31)。
3、getDay()
——從 Date 對(duì)象返回一周中的某一天 (0 ~ 6)。
4、getMonth()
——從 Date 對(duì)象返回月份 (0 ~ 11)。
5、getFullYear()
——從 Date 對(duì)象以四位數(shù)字返回年份。
6、getYear()
——請(qǐng)使用 getFullYear() 方法代替。
7、getHours()
——返回 Date 對(duì)象的小時(shí) (0 ~ 23)。
8、getMinutes()
——返回 Date 對(duì)象的分鐘 (0 ~ 59)。
9、getSeconds()
——返回 Date 對(duì)象的秒數(shù) (0 ~ 59)。
10、getMilliseconds()
——返回 Date 對(duì)象的毫秒(0 ~ 999)
11、getTime()
——返回 1970 年 1 月 1 日至今的毫秒數(shù)。
設(shè)置日期
1、setDate()
——設(shè)置 Date 對(duì)象中月的某一天 (1 ~ 31)。
2、setMonth()
——設(shè)置 Date 對(duì)象中月份 (0 ~ 11)。
3、setFullYear()
——設(shè)置 Date 對(duì)象中的年份(四位數(shù)字)。
4、setHours()
——設(shè)置 Date 對(duì)象中的小時(shí) (0 ~ 23)。
5、setMinutes()
——設(shè)置 Date 對(duì)象中的分鐘 (0 ~ 59)。
6、setSeconds()
——設(shè)置 Date 對(duì)象中的秒鐘 (0 ~ 59)。
7、setMilliseconds()
——設(shè)置 Date 對(duì)象中的毫秒 (0 ~ 999)。
8、setTime()
——以毫秒設(shè)置 Date 對(duì)象。
--------------------------------------------------
JS Date 對(duì)象用于處理日期和時(shí)間。
創(chuàng)建 Date 對(duì)象的語法:
var myDate=new Date()
Date 對(duì)象會(huì)自動(dòng)把當(dāng)前日期和時(shí)間保存為其初始值。
參數(shù)形式有以下5種:
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);
注意最后一種形式,參數(shù)表示的是需要?jiǎng)?chuàng)建的時(shí)間和GMT時(shí)間1970年1月1日之間相差的毫秒數(shù)。各種函數(shù)的含義如下:
month:用英文表示月份名稱,從January到December
mth:用整數(shù)表示月份,從0-11(1月到12月)
dd:表示一個(gè)月中的第幾天,從1到31
yyyy:四位數(shù)表示的年份
hh:小時(shí)數(shù),從0(午夜)到23(晚11點(diǎn))
mm:分鐘數(shù),從0到59的整數(shù)
ss:秒數(shù),從0到59的整數(shù)
ms:毫秒數(shù),為大于等于0的整數(shù)
如:
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);
Date() 返回當(dāng)日的日期和時(shí)間。
getDate() 從 Date 對(duì)象返回一個(gè)月中的某一天 (1 ~ 31)。
getDay() 從 Date 對(duì)象返回一周中的某一天 (0 ~ 6)。
getMonth() 從 Date 對(duì)象返回月份 (0 ~ 11)。
getFullYear() 從 Date 對(duì)象以四位數(shù)字返回年份。
getYear() 請(qǐng)使用 getFullYear() 方法代替。
getHours() 返回 Date 對(duì)象的小時(shí) (0 ~ 23)。
getMinutes() 返回 Date 對(duì)象的分鐘 (0 ~ 59)。
getSeconds() 返回 Date 對(duì)象的秒數(shù) (0 ~ 59)。
getMilliseconds() 返回 Date 對(duì)象的毫秒(0 ~ 999)。
getTime() 返回 1970 年 1 月 1 日至今的毫秒數(shù)。
getTimezoneOffset() 返回本地時(shí)間與格林威治標(biāo)準(zhǔn)時(shí)間 (GMT) 的分鐘差。
getUTCDate() 根據(jù)世界時(shí)從 Date 對(duì)象返回月中的一天 (1 ~ 31)。
getUTCDay() 根據(jù)世界時(shí)從 Date 對(duì)象返回周中的一天 (0 ~ 6)。
getUTCMonth() 根據(jù)世界時(shí)從 Date 對(duì)象返回月份 (0 ~ 11)。
getUTCFullYear() 根據(jù)世界時(shí)從 Date 對(duì)象返回四位數(shù)的年份。
getUTCHours() 根據(jù)世界時(shí)返回 Date 對(duì)象的小時(shí) (0 ~ 23)。
getUTCMinutes() 根據(jù)世界時(shí)返回 Date 對(duì)象的分鐘 (0 ~ 59)。
getUTCSeconds() 根據(jù)世界時(shí)返回 Date 對(duì)象的秒鐘 (0 ~ 59)。
getUTCMilliseconds() 根據(jù)世界時(shí)返回 Date 對(duì)象的毫秒(0 ~ 999)。
parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒數(shù)。
setDate() 設(shè)置 Date 對(duì)象中月的某一天 (1 ~ 31)。
setMonth() 設(shè)置 Date 對(duì)象中月份 (0 ~ 11)。
setFullYear() 設(shè)置 Date 對(duì)象中的年份(四位數(shù)字)。
setYear() 請(qǐng)使用 setFullYear() 方法代替。
setHours() 設(shè)置 Date 對(duì)象中的小時(shí) (0 ~ 23)。
setMinutes() 設(shè)置 Date 對(duì)象中的分鐘 (0 ~ 59)。
setSeconds() 設(shè)置 Date 對(duì)象中的秒鐘 (0 ~ 59)。
setMilliseconds() 設(shè)置 Date 對(duì)象中的毫秒 (0 ~ 999)。
setTime() 以毫秒設(shè)置 Date 對(duì)象。
setUTCDate() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中月份的一天 (1 ~ 31)。
setUTCMonth() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的月份 (0 ~ 11)。
setUTCFullYear() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的年份(四位數(shù)字)。
setUTCHours() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的小時(shí) (0 ~ 23)。
setUTCMinutes() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的分鐘 (0 ~ 59)。
setUTCSeconds() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的秒鐘 (0 ~ 59)。
setUTCMilliseconds() 根據(jù)世界時(shí)設(shè)置 Date 對(duì)象中的毫秒 (0 ~ 999)。
toSource() 返回該對(duì)象的源代碼。
toString() 把 Date 對(duì)象轉(zhuǎn)換為字符串。
toTimeString() 把 Date 對(duì)象的時(shí)間部分轉(zhuǎn)換為字符串。
toDateString() 把 Date 對(duì)象的日期部分轉(zhuǎn)換為字符串。
toGMTString() 請(qǐng)使用 toUTCString() 方法代替。
toUTCString() 根據(jù)世界時(shí),把 Date 對(duì)象轉(zhuǎn)換為字符串。
toLocaleString() 根據(jù)本地時(shí)間格式,把 Date 對(duì)象轉(zhuǎn)換為字符串。
toLocaleTimeString() 根據(jù)本地時(shí)間格式,把 Date 對(duì)象的時(shí)間部分轉(zhuǎn)換為字符串。
toLocaleDateString() 根據(jù)本地時(shí)間格式,把 Date 對(duì)象的日期部分轉(zhuǎn)換為字符串。
UTC() 根據(jù)世界時(shí)返回 1997 年 1 月 1 日 到指定日期的毫秒數(shù)。
valueOf() 返回 Date 對(duì)象的原始值。
//var objDate=new Date([arguments list]);
相關(guān)文章
JavaScript中匿名、命名函數(shù)的性能測(cè)試
這篇文章主要介紹了JavaScript中匿名、命名函數(shù)的性能測(cè)試,簡(jiǎn)單來講匿名即沒有名字的函數(shù),它和命名函數(shù)相比性能如何呢,本文即是對(duì)它們性能的測(cè)試,需要的朋友可以參考下2014-09-09JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇示例介紹
這篇文章主要介紹了JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇,需要的朋友可以參考下2014-06-06在微信、支付寶、百度錢包實(shí)現(xiàn)點(diǎn)擊返回按鈕關(guān)閉當(dāng)前頁面和窗口的方法
這篇文章主要介紹了在微信、支付寶、百度錢包實(shí)現(xiàn)點(diǎn)擊返回按鈕關(guān)閉當(dāng)前頁面和窗口的方法,傳統(tǒng)的window.close()是無效的,必須要使用它們的js代碼才能關(guān)閉。下面小編給大家分享下代碼,一起看看吧2016-08-08javascript開發(fā)技術(shù)大全-第3章 js數(shù)據(jù)類型
字符串類型(string) :由unicode字符、數(shù)字、標(biāo)點(diǎn)符號(hào)組成,在javascript中沒有char字符類型 ,即使只表示一個(gè)字符,也必須用到字符串2011-07-07Javascript實(shí)現(xiàn)的SHA-256加密算法完整實(shí)例
這篇文章主要介紹了Javascript實(shí)現(xiàn)的SHA-256加密算法,以完整實(shí)例形式分析了JavaScript實(shí)現(xiàn)SHA-256加密的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02js關(guān)于字符長(zhǎng)度限制的問題示例探討
字符長(zhǎng)度限制在某些特殊的使用場(chǎng)合還是比較實(shí)用的,下面為大家用示例探討下,感興趣的朋友可以參考下2014-01-01Javascript Math ceil()、floor()、round()三個(gè)函數(shù)的區(qū)別
Round是四舍五入的。。。Ceiling是向上取整。。float是向下取整2010-03-03深入解讀JavaScript中的Hoisting機(jī)制
這篇文章主要介紹了JavaScript中的Hoisting機(jī)制,涉及到JS中變量聲明的相關(guān)問題,需要的朋友可以參考下2015-08-08