JavaScript Tutorial - 3

I know ... everything so far could have been done without JavaScript - JavaScript just gave us some shortcuts. So let's move on to stuff that can't be done without Javascript.

Did you know that JavaScript knows what day it is? Try This:

<html> <head> <title>Now</title> </head> <body> <script> var now=new Date() document.write(now) </script> </body> </html> But we can go further than this - we can pick out parts of the date.
For example, replace (now) by (now.getYear()) and see what happens.
Try:
  • now.getMonth()
  • now.getDate()
  • now.getDay()
  • now.getHours()
  • now.getMinutes()
You would have noticed how everything returns a number.
This date stuff provides us with a great opportunity to learn about another great computer concept (loops was the first one) which is making decisions.
I mean being able to say to the computer, "if this is true then do that".
Let me give you a simple example:
if (now.getDay()==0) { document.write("Sunday") } Task 6: Write a script to display what day it is. Check.

Task 7: Write a script to display what month it is. Check.

Challenge 4: Write a script to display the hour in am/pm form (eg 11:45am) - there are all sorts of "gotchas" in this challenge question - test very thoroughly before sending me your answer.

HOME Table of Contents Previous Next