javascript學(xué)習(xí)筆記(六) Date 日期類型
更新時間:2012年06月19日 23:04:15 作者:
javascript學(xué)習(xí)筆記之Date 日期類型介紹,需要的朋友可以參考下
1.創(chuàng)建日期對象
var now = new Date(); //獲得當(dāng)前系統(tǒng)日期和時間
var someDate = new Date(Date.parse("May 25,2012"));
var someDate = new Date("May 25,2012"); //與上相同
var someDate = new Date(Date.UTC(2010,0)); //GMT時間2010年1月1日凌晨0時
var someDate = new Date(2010,0); //與上相同
var someDate = new Date(Date.UTC(2010,4,5,17,55,55)); //GMT時間2010年5月日下午5:55:55,月和時以0為基數(shù),0表示一月
var someDate = new Date(2010,4,5,17,55,55); //與上相同
Date.parse()和Date.UTC()返回相應(yīng)日期的毫秒數(shù),Date.UTC()參數(shù)分別是:年、月、日、時、分、秒、毫秒.至少有年和月2個參數(shù).其中月和時以0為基數(shù)
2.日期的比較
var date1 = new Date(2007,0,1);
var date2 = new Date(2007,1,1);
alert(date1>date2); //false
alert(date1<date2); //true
3.日期格式化方法及獲得設(shè)置日期方法
View Code
toDateString()
toTimeString()
toLocaleDateString() //如2007年1月1日
toLocaleTimeString() //如13:55:55
toUTCString()
getTime() //返回日期的毫秒數(shù)
setTime()
getFullYear() //獲得年份,如2007
setFullYear()
getMonth() //獲得月份,0表示一月
setMonth() //設(shè)置月份,0表示一月,超過11增加年份
getDate() //獲得日期中的天數(shù)
setDate() //設(shè)置天數(shù),超過31增加月份
getDay() //獲得星期,0表示星期日,6表示星期六
getHours() //獲得小時,0~23
setHours() //設(shè)置小時,0~23
getMinutes() //獲得分鐘,0~59
setMinutes() //設(shè)置分鐘,0~59
getSeconds() //獲得秒數(shù),0~59
setSeconds() //設(shè)置秒數(shù),0~59
getMilliseconds() //獲得毫秒
setMilliseconds() //設(shè)置毫秒
復(fù)制代碼 代碼如下:
var now = new Date(); //獲得當(dāng)前系統(tǒng)日期和時間
var someDate = new Date(Date.parse("May 25,2012"));
var someDate = new Date("May 25,2012"); //與上相同
var someDate = new Date(Date.UTC(2010,0)); //GMT時間2010年1月1日凌晨0時
var someDate = new Date(2010,0); //與上相同
var someDate = new Date(Date.UTC(2010,4,5,17,55,55)); //GMT時間2010年5月日下午5:55:55,月和時以0為基數(shù),0表示一月
var someDate = new Date(2010,4,5,17,55,55); //與上相同
Date.parse()和Date.UTC()返回相應(yīng)日期的毫秒數(shù),Date.UTC()參數(shù)分別是:年、月、日、時、分、秒、毫秒.至少有年和月2個參數(shù).其中月和時以0為基數(shù)
2.日期的比較
復(fù)制代碼 代碼如下:
var date1 = new Date(2007,0,1);
var date2 = new Date(2007,1,1);
alert(date1>date2); //false
alert(date1<date2); //true
3.日期格式化方法及獲得設(shè)置日期方法
復(fù)制代碼 代碼如下:
View Code
toDateString()
toTimeString()
toLocaleDateString() //如2007年1月1日
toLocaleTimeString() //如13:55:55
toUTCString()
getTime() //返回日期的毫秒數(shù)
setTime()
getFullYear() //獲得年份,如2007
setFullYear()
getMonth() //獲得月份,0表示一月
setMonth() //設(shè)置月份,0表示一月,超過11增加年份
getDate() //獲得日期中的天數(shù)
setDate() //設(shè)置天數(shù),超過31增加月份
getDay() //獲得星期,0表示星期日,6表示星期六
getHours() //獲得小時,0~23
setHours() //設(shè)置小時,0~23
getMinutes() //獲得分鐘,0~59
setMinutes() //設(shè)置分鐘,0~59
getSeconds() //獲得秒數(shù),0~59
setSeconds() //設(shè)置秒數(shù),0~59
getMilliseconds() //獲得毫秒
setMilliseconds() //設(shè)置毫秒
相關(guān)文章
關(guān)于JS字符串函數(shù)String.replace()
本篇介紹關(guān)于JS字符串函數(shù)String.replace(),有需要的朋友參考一下。2013-04-04JavaScript顯式數(shù)據(jù)類型轉(zhuǎn)換詳解
這篇文章主要介紹了JavaScript顯式數(shù)據(jù)類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03JavaScript中用getDate()方法返回指定日期的教程
這篇文章主要介紹了JavaScript中用getDate()方法返回指定日期的教程,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06javascript基礎(chǔ)知識分享之類與函數(shù)化
在C++中是以class來聲明一個類的,JavaScript與C++不同,它使用了與函數(shù)一樣的function來聲明,這就讓許多學(xué)Jscript的朋友把類與函數(shù)混在一起了,在Jscript中函數(shù)與類確實有些混,但使用久了自然會理解,這篇文章是針對想進(jìn)攻面向?qū)ο缶幊痰呐笥讯鴮?就不打算討論得太深了2016-02-02