国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript中Date對象的常用方法示例

2024-05-06 16:24:50
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript中Date對象的常用方法示例,Date對象的相關運用是JS入門學習中的基礎知識,需要的朋友可以參考下

getFullYear()

使用 getFullYear() 獲取年份。

源代碼:

 

 
  1. </script> 
  2. <!DOCTYPE html> 
  3. <html> 
  4. <body> 
  5. <p id="demo">Click the button to display the full year of todays date.</p> 
  6. <button onclick="myFunction()">Try it</button> 
  7. <script> 
  8. function myFunction() 
  9. var d = new Date(); 
  10. var x = document.getElementById("demo"); 
  11. x.innerHTML=d.getFullYear(); 
  12. </script> 
  13. </body> 
  14. </html>  

測試結果:

 

 
  1. 2015 

getTime()

getTime() 返回從 1970 年 1 月 1 日至今的毫秒數。

源代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <body> 
  4. <p id="demo">Click the button to display the number of milliseconds since midnight, January 1, 1970.</p> 
  5. <button onclick="myFunction()">Try it</button> 
  6. <script> 
  7. function myFunction() 
  8. var d = new Date(); 
  9. var x = document.getElementById("demo"); 
  10. x.innerHTML=d.getTime(); 
  11. </script> 
  12. </body> 
  13. </html>  

測試結果:

 

 
  1. 1445669203860 

setFullYear()

如何使用 setFullYear() 設置具體的日期。

源代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <body> 
  4. <p id="demo">Click the button to display a date after changing the year, month, and day.</p> 
  5. <button onclick="myFunction()">Try it</button> 
  6. <script> 
  7. function myFunction() 
  8. var d = new Date(); 
  9. d.setFullYear(2020,10,3); 
  10. var x = document.getElementById("demo"); 
  11. x.innerHTML=d; 
  12. </script> 
  13. <p>Remember that JavaScript counts months from 0 to 11. 
  14. Month 10 is November.</p> 
  15. </body> 
  16. </html>  

測試結果:

 

 
  1. Tue Nov 03 2020 14:47:46 GMT+0800 (中國標準時間) 

toUTCString()

如何使用 toUTCString() 將當日的日期(根據 UTC)轉換為字符串。

源代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <body> 
  4. <p id="demo">Click the button to display the UTC date and time as a string.</p> 
  5. <button onclick="myFunction()">Try it</button> 
  6. <script> 
  7. function myFunction() 
  8. var d = new Date(); 
  9. var x = document.getElementById("demo"); 
  10. x.innerHTML=d.toUTCString(); 
  11. </script> 
  12. </body> 
  13. </html>  

測試結果:

 

 
  1. Sat, 24 Oct 2015 06:49:05 GMT 

getDay()

如何使用 getDay() 和數組來顯示星期,而不僅僅是數字。

源代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <body> 
  4. <p id="demo">Click the button to display todays day of the week.</p> 
  5. <button onclick="myFunction()">Try it</button> 
  6. <script> 
  7. function myFunction() 
  8. var d = new Date(); 
  9. var weekday=new Array(7); 
  10. weekday[0]="Sunday"
  11. weekday[1]="Monday"
  12. weekday[2]="Tuesday"
  13. weekday[3]="Wednesday"
  14. weekday[4]="Thursday"
  15. weekday[5]="Friday"
  16. weekday[6]="Saturday"
  17. var x = document.getElementById("demo"); 
  18. x.innerHTML=weekday[d.getDay()]; 
  19. </script> 
  20. </body> 
  21. </html> 

測試結果:

 

 
  1. Saturday 

Display a clock

如何在網頁上顯示一個鐘表。

源代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <script> 
  5. function startTime() 
  6. var today=new Date(); 
  7. var h=today.getHours(); 
  8. var m=today.getMinutes(); 
  9. var s=today.getSeconds(); 
  10. // add a zero in front of numbers<10 
  11. m=checkTime(m); 
  12. s=checkTime(s); 
  13. document.getElementById('txt').innerHTML=h+":"+m+":"+s; 
  14. t=setTimeout(function(){startTime()},500); 
  15. function checkTime(i) 
  16. if (i<10) 
  17. i="0" + i; 
  18. return i; 
  19. </script> 
  20. </head> 
  21. <body onload="startTime()"
  22. <div id="txt"></div> 
  23. </body> 
  24. </html>  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 洪雅县| 昔阳县| 平果县| 麻江县| 增城市| 镇赉县| 长治县| 尚义县| 喀什市| 天峻县| 北海市| 金华市| 景泰县| 鹤山市| 达拉特旗| 遂溪县| 甘洛县| 香河县| 泸溪县| 申扎县| 虹口区| 荔浦县| 新乡县| 开江县| 莱西市| 林州市| 汾西县| 旬阳县| 咸阳市| 河东区| 文化| 广水市| 和硕县| 枣阳市| 和平区| 桦甸市| 姜堰市| 青阳县| 赞皇县| 凤翔县| 建始县|