首页 > 语言 > 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. var d = new Date(); 
  13. var x = document.getElementById("demo"); 
  14. x.innerHTML=d.getFullYear(); 
  15. </script> 
  16. ​ 
  17. </body> 
  18. </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. var d = new Date(); 
  12. var x = document.getElementById("demo"); 
  13. x.innerHTML=d.getTime(); 
  14. </script> 
  15. ​ 
  16. </body> 
  17. </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. var d = new Date(); 
  12. d.setFullYear(2020,10,3); 
  13. var x = document.getElementById("demo"); 
  14. x.innerHTML=d; 
  15. </script> 
  16. ​ 
  17. <p>Remember that JavaScript counts months from 0 to 11. 
  18. Month 10 is November.</p> 
  19. </body> 
  20. </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. var d = new Date(); 
  12. var x = document.getElementById("demo"); 
  13. x.innerHTML=d.toUTCString(); 
  14. </script> 
  15. ​ 
  16. </body> 
  17. </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. var d = new Date(); 
  12. var weekday=new Array(7); 
  13. weekday[0]="Sunday"
  14. weekday[1]="Monday"
  15. weekday[2]="Tuesday"
  16. weekday[3]="Wednesday"
  17. weekday[4]="Thursday"
  18. weekday[5]="Friday"
  19. weekday[6]="Saturday"
  20. ​ 
  21. var x = document.getElementById("demo"); 
  22. x.innerHTML=weekday[d.getDay()]; 
  23. </script> 
  24. ​ 
  25. </body> 
  26. </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. ​ 
  16. function checkTime(i) 
  17. if (i<10) 
  18. i="0" + i; 
  19. return i; 
  20. </script> 
  21. </head> 
  22. ​ 
  23. <body onload="startTime()"
  24. <div id="txt"></div> 
  25. </body> 
  26. </html>  

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

图片精选