Javascript - Dates (Now, 10 years ago, week from today)


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>Solution 11.2</title>
  4. <script type = "text/javascript">
  5. function printNow()
  6. {
  7. var curr = new Date();
  8. window.alert("The time and date are: " + curr.toString());
  9. }
  10.  
  11. function printYesterday()
  12. {
  13. var yesterday = new Date();
  14. yesterday.setTime(yesterday.valueOf() - 24 * 60 * 60 * 1000);
  15. window.alert("24 hours age, the time and date were: " + yesterday.toString());
  16. }
  17.  
  18. function printTenYears()
  19. {
  20. var tenYears = new Date();
  21. tenYears.setTime(tenYears.valueOf() - 10 * 365 * 24 * 60 * 60 * 1000);
  22. window.alert("10 years ago, the time and date were: " + tenYears.toString());
  23. }
  24.  
  25. function printOneWeek()
  26. {
  27. var oneWeek = new Date();
  28. oneWeek.setTime(oneWeek.valueOf() + 7 * 24 * 60 * 60 * 1000);
  29. window.alert("In one week, the time and date will be: " + oneWeek.toString());
  30. }
  31. </script>
  32. </head>
  33.  
  34. <body>
  35. <form action = "">
  36. <div>
  37. <input type="button" value="Now" onclick="printNow()" />
  38. <input type="button" value="Yesterday" onclick="printYesterday()" />
  39. <input type="button" value="Ten Years Ago" onclick="printTenYears()" />
  40. <input type="button" value="In One Week" onclick="printOneWeek()" />
  41. </div>
  42. </form>
  43. </body>
  44.  
  45. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.