JavaScript Lesson 8 (Dates and Timers) - Homework

Q1

Write a program which displays the full date and time, and then displays the (updated) date and time exactly 5 minutes later.

Q2

What should be displayed by this program?

<script language="JavaScript">
var dte = new Date("4 June 2001");
var d = dte.getDate();
var m = dte.getMonth();
var y = dte.getFullYear();
dte.setFullYear(y + 150);
dte.setMonth(m + 150);
dte.setDate(d + 150);
document.write(dte);
</script>

Q3

Write a program which allows you to enter a reminder message and a time delay (say 10 minutes). The program should display the message in an alert box after the specified delay. Does the message still appear even if you have left that particular web page and started browsing on the Internet?

Q4

The following two programs don't produce the same result - or do they?

 <script language="JavaScript">
 var d = new Date();
 var x = d.getMonth();
 var y = d.getDate();
 d.setMonth(x + 400);
 d.setDate(y + 150);
 alert(d);
 </script>
 
 <script language="JavaScript">
 var d = new Date();
 d.setMonth(d.getMonth() + 400);
 d.setDate(d.getDate() + 150);
 alert(d);
 </script>
 

Q5

How many minutes seconds have elapsed since the start of the new century? (You may decide whether this is midnight on the 1st of January 2000, or midnight on the first of January 2001.)

Q6

Write a program which tells you on which day of the week Christmas Day (25th December) will fall each year from now until 2099.

Q7

Write a program that invites the user to estimate one minute of time. The user should click on a button on the screen, and then click on the button again after what he/she estimates to be one minute. The program then displays the true length of time. If the user still hasn't clicked on the button after 5 minutes have elapsed, the program should display a "Ah! Come on!" message.

Q8

Create an electronic stopwatch program which allows a user to click on a button to start the stopwatch and then click on the same button to stop it again. The stopwatch should then inform the user how much time has elapsed between the two button clicks - both in milliseconds, and also in seconds and minutes.

Q9

How many days have elapsed since

  • the death of Beethoven, on the 23rd March 1827?

  • the dropping of the atom bomb on 6th August 1945?
  • On which days of the week did each of these events happen?

    Q10

    Traditionally, Easter Sunday always takes place exactly 40 days after Ash Wednesday. Write a program which will accept the date of Ash Wednesday (i.e. as a month and a day) and will give you the date of Easter Sunday. Now adapt the program so that you can enter either date (Ash Wednesday or Easter Sunday) and the program will give you the other date. As a double check, the program should determine which day of the week the day you entered was, and report an error if the date entered was not a Wednesday/Sunday (as appropriate).