Javascript date and time
Javascript provides the functionality to display date and time on a webpage.
NOTE: Whenever you display the time or date on a webpage using Javascript, it will get that data from whatever the user has on their computer, whether it is correct or not. So if the current date and time is January 15th 12:30, but the users computer is set to January 10th 8:30, that is what will be displayed.
This tutorial focuses on:
- The Date object
- Displaying a date on a wepage
- Displaying time on a wepage
- Displaying date and time on a wepage
The Date object
The Date object is used to display date and time on a webpage.
Defining a Date object
NOTE: The Date object stores the current date and time by default.
Date object methods
- getDate() - Returns the day of the month in numerical form (1 - 31)
- getDay() - Returns the day of the month in numerical form (0 - 6)
- getHours() - Returns the current hour (0 -23)
- getMinutes() - Returns the current minute (0- 59)
- getSeconds() - Returns the current seconds (0 - 59)
Displaying a date on a webpage
You can display a date on a webpage using the appropriate methods of the Date object needed to do this:
Displaying time on a webpage
You can display time on a webpage using the appropriate methods of the Date object needed to do this:
Displaying date and time on a webpage
You can display the current date and time together on a webpage using just the Date object without any methods since the Date object stores the current date and time by default: