Thursday, January 3, 2008

Displaying date/time in JavaScript

For JavaScript beginners, getting/displaying the current time can be a little more involved than expected. This is because JavaScript contains a bunch of date/time functions that you can use depending on what part of the date you need.

For example, the getDate() function retrieves the day of the month. It doesn't retrieve the whole date - just the day of the month (i.e. 1 - 31). getDay() on the other hand, retrieves the day of the week.

These are just two of many date/time functions. What you need to do is use whichever JavaScript date/time function suits your purpose, then (still using JavaScript), massage the output as necessary.

Quackit's JavaScript tutorial demonstrates how you can display the date and time using JavaScript. So, you can simply copy and paste the code if you like. After all, displaying the date and time shouldn't be so hard!

No comments: