JavaScript Date and Time

You can use JavaScript to display (or use within a script) the users' local date and time.

JavaScript provides you with the ability to access the date and time of your users' local computer, which can be quite useful at times.

Displaying the current date and time in a nice user friendly way using JavaScript is not quite as simple as you might like. You need to massage the data a little. You can do this using a bunch of JavaScript date and time functions.

Here are some examples.

Displaying the Current Date

Displaying the Current Time

You may have noticed that we had to add a leading zero to the minutes part of the date if it was less than 10. By default, JavaScript doesn't display the leading zero. Also, you might have noticed that the time is being displayed in 24 hour time format. This is how JavaScript displays the time. If you need to display it in AM/PM format, you need to convert it.

Displaying the Current Time in AM/PM Format

JavaScript Date and Time Functions

For a full list of JavaScript date and time functions, see JavaScript Date and Time Functions