JavaScript&jQuery.Date對象和Time對象

來源:互聯網
上載者:User

標籤: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對象?

答:

  1. new Date(“month dd,yyyy hh:mm:ss”);
  2. new Date(“month dd,yyyy”);
  3. new Date(yyyy,mth,dd,hh,mm,ss);
  4. new Date(yyyy,mth,dd);
  5. 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對象

聯繫我們

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