首页 > 语言 > 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. ​ 
  6. <p id="demo">Click the button to display the full year of todays date.</p> 
  7. ​ 
  8. <button onclick="myFunction()">Try it</button> 
  9. ​ 
  10. <script> 
  11. function myFunction() 
  12. { 
  13. var d = new Date(); 
  14. var x = document.getElementById("demo"); 
  15. x.innerHTML=d.getFullYear(); 
  16. } 
  17. </script> 
  18. ​ 
  19. </body> 
  20. </html>  

测试结果:

 

 
  1. 2015 

getTime()

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

源代码:

 

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

测试结果:

 

 
  1. 1445669203860 

setFullYear()

如何使用 setFullYear() 设置具体的日期。

源代码:

 

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

测试结果:

 

 
  1. Tue Nov 03 2020 14:47:46 GMT+0800 (中国标准时间) 

toUTCString()

如何使用 toUTCString() 将当日的日期(根据 UTC)转换为字符串。

源代码:

 

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

测试结果:

 

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

getDay()

如何使用 getDay() 和数组来显示星期,而不仅仅是数字。

源代码:

 

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

测试结果:

 

 
  1. Saturday 

Display a clock

如何在网页上显示一个钟表。

源代码:

 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选