1.獲取系統(tǒng)當(dāng)前時間
var?myDate =?new?Date();//獲取系統(tǒng)當(dāng)前時間
2.獲取特定格式的時間
myDate.getYear(); //獲取當(dāng)前年份(2位)
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
myDate.getMonth(); //獲取當(dāng)前月份(0-11,0代表1月)
myDate.getDate(); //獲取當(dāng)前日(1-31)
myDate.getDay(); //獲取當(dāng)前星期X(0-6,0代表星期天)
myDate.getTime(); //獲取當(dāng)前時間(從1970.1.1開始的毫秒數(shù))
myDate.getHours(); //獲取當(dāng)前小時數(shù)(0-23)
myDate.getMinutes(); //獲取當(dāng)前分鐘數(shù)(0-59)
myDate.getSeconds(); //獲取當(dāng)前秒數(shù)(0-59)
myDate.getMilliseconds(); //獲取當(dāng)前毫秒數(shù)(0-999)
myDate.toLocaleDateString(); //獲取當(dāng)前日期
var mytime=myDate.toLocaleTimeString(); //獲取當(dāng)前時間
myDate.toLocaleString( ); //獲取日期與時間
3.獲取當(dāng)前時間戳
(1)var timestamp =Date.parse(new Date());
注:得到的結(jié)果:1280977330000 注意:這里得到的結(jié)果將后三位(毫秒)轉(zhuǎn)換成了000顯示,使用時可能會出現(xiàn)問題。例如動態(tài)添加頁面元素id的時候,不建議使用。
(2)vartimestamp =(newDate()).valueOf();
結(jié)果:1280977330748
(3)vartimestamp=newDate().getTime();
結(jié)果:1280977330748
注:
? ? ?js中單獨調(diào)用new Date(),例如document.write(new Date());顯示的結(jié)果是:Mar 31 10:10:43 UTC+0800 2012 這種格式的時間
? ? ?但是用new Date() 參與計算會自動轉(zhuǎn)換為從1970.1.1開始的毫秒數(shù)。
總結(jié): js 獲取當(dāng)前年月日時分秒星期
$("#aa").click(function () {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var currentTime = "現(xiàn)在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
alert(currentTime);
});
W3C的Date具體講解:? ? http://www.w3school.com.cn/jsref/jsref_obj_date.asp