The Date Object Constructor
Below is an example of how to create a new Date Object using the Date Object Constructor. Once we've created a date object and assigned a variable name to it, the date object methods, "
getDay(), getMonth(), getDate() and
getYear()" are used to pull the date information from the user's computer.
var todaysDate = new Date()
// Create a new Date Object
var theDay = todaysDate.getDay()
// getDay() returns integer of 0-6 which will be stored in the variable theDay
var theMonth = todaysDate.getMonth()
// getMonth() returns integer of 0-11 which will be stored in the variable theMonth
var theDate = todaysDate.getDate()
// getDate() returns day of the month which will be stored in the variable theDate
var theYear = todaysDate.getYear()
// getYear() returns year which will be stored in the variable theYear
if (theYear < 2000) theYear += 1900
// fix for y2k bug (in some browsers) when using getYear() method. if theYear is less than 2000, theYear is equal to itself plus 1900
Date methods used to find time are
getHours() (returns 0-23),
getMinutes() (returns 0-59) and
getSeconds() (returns 0-59).
JavaScript returns an integer for the values of the date object methods. Since integers are how we refer to days of the month (the 15th) and the year, the values returned for getDate() and getYear() are fine.
But since no one refers to Sunday as "0", or December as "11" (JavaScript begins counting at zero), we'll need to change those values to meaningful strings. We could use if/else statements, but there is an easier way.
View the Source