標籤:jquer cond 產生 gets ring comment mat plain document
Date對象和Time對象
Date對象用於操作日期
Time對象用於操作時間
Date對象在使用時前需要先執行個體化,採用建構函式執行個體化。
var today=new Date();
// 返回年份
document.write(today.getFullYear());document.write(‘<br>‘);// 返回月份,從0到11,0表示1月,11表示12月document.write(today.getMonth());document.write(‘<br>‘);// 返回日期,具體某日document.write(today.getDate());document.write(‘<br>‘);// 返回星期幾document.write(today.getDay());document.write(‘<br>‘);// 返回小時document.write(today.getHours());document.write(‘<br>‘);// 返回分document.write(today.getMinutes());document.write(‘<br>‘);// 返回秒document.write(today.getSeconds());document.write(‘<br>‘);// 返回毫秒,毫秒的範圍從~999document.write(today.getMilliseconds());document.write(‘<br>‘);// 返回時間,這個時間是UTC開始計算的秒數,人看不懂document.write(today.getTime());document.write(‘<br>‘);// toTimeString 將時間轉換成人看得懂的document.write(today.toTimeString(today.getTime()));document.write(‘<br>‘);產生指定的日期var lastDate=new Date(‘1998-12-12T12:23:45‘);document.write(lastDate.toDateString(lastDate.getTime()));document.write(‘<br>‘);document.write(lastDate.toTimeString(lastDate.getTime()));document.write(‘<br>‘);
測試題目
1、如何建立Date對象?
答:
- 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);
2、Date對象、Number對象、String對象、Math對象的區別是什嗎?
答:String 對象的屬性和方法用於操作字串。
Number對象通常用於運算元字。
Math對象用於數學上的計算,如算平方,四捨五入,產生隨機數等等。
Date對象用於操作日期
3、如何使用Date對象產生一個指定的日期?
var lastDate=new Date(‘1998-12-12T12:23:45‘);
document.write(lastDate.toDateString(lastDate.getTime()));document.write(‘<br>‘);document.write(lastDate.toTimeString(lastDate.getTime()));document.write(‘<br>‘);
4、如何計算兩個日期之間的差距?
JavaScript&jQuery.Date對象和Time對象