Forum Discussion
Update a variable with the current date and time
In JavaScript you can create date & time objects using the Date constructor. Then object can use its own methods for different computations afterward.
There are number of ways in which you can create the date object via Date constructor.
Example
var today_date_time = new Date(); // Get today's date and time
var old_date = new Date(2010, 4, 1); // Date Literal
var old_date_time = new Date(2010, 0, 1, 18, 10, 30); // Date literal + time
var elapsed = today_date_time - old_date ; // Get the interval after subtraction in milliseconds
// Methodsold_date.getFullYear() // get year
old_date.getMonth() // get month number (starting from zero)
old_date.getDate() // get day
old_date.getDay() // get day of week (0-6) Starting from Sunday: 0
old_date.getHours() // get local time
old_date.getUTCHours() // get time in UTC
See working example at:
Related Content
- 10 months ago
- 10 months ago
- 10 months ago