JavaScript Programming

Date
Returns today's date including date, month, and year. Note that the getMonth method returns 0 in January, 1 in February etc. So add 1 to the getMonth method to display the correct date.

Time
Returns the current local time including hour, minutes, and seconds. To return the GMT time use getUTCHours, getUTCMinutes etc.

Set date
You can also set the date or time into the date object, with the setDate, setHour etc. Note that in this example, only the FullYear is set.

UTC time
The getUTCDate method returns the Universal Coordinated Time which is the time set by the World Time Standard.

Display weekday
A simple script that allows you to write the name of the current day instead of the number. Note that the array object is used to store the names, and that Sunday=0, Monday=1 etc.

Display full date
How to write a complete date with the name of the day and the name of the month.

Display time
How to display the time on your pages. Note that this script is similar to the Time example above, only this script writes the time in an input field. And it continues writing the time one time per second.


Date Object

The Date object is used to work with dates and times. 

To create an instance of the Date object and assign it to a variable called "d", you do the following:

var d=new Date()

After creating an instance of the Date object, you can access all the methods of the Date object from the "d" variable.

To return the current day in a month (from 1-31) of a Date object, write the following:

d.getDate()

The Date object can also have the following parameters:

new Date(milliseconds)
new Date(dateString)
new Date(yr_num, mo_num, day_num [, hr_num, min_num, sec_num, ms_num])

If you only use Date(), JavaScript creates an object for today's date according to the time on the local machine.

Here are some examples on how to create Date objects:

var d=new Date("October 12, 1988 13:14:00")
var d=new Date("October 12, 1988")
var d=new Date(88,09,12,13,14,00)
var d=new Date(88,09,12)
var d=new Date(500)

The Date object's properties and methods are described below:

NN: Netscape, IE: Internet Explorer

Properties

Syntax: object.property_name

Property Description NN IE 
constructor Contains the function that created an object's prototype 4 4
prototype Allows addition of properties to a date 3 4

Methods

Syntax: object.method_name()

Method Description NN IE
Date() Returns a Date object 2 3
getDate() Returns the date of a Date object (from 1-31) 2 3
getDay() Returns the day of a Date object (from 0-6. 0=Sunday, 1=Monday, etc.) 2 3
getMonth() Returns the month of a Date object (from 0-11. 0=January, 1=February, etc.) 2 3
getFullYear() Returns the year of a Date object (four digits) 4 4
getYear() Returns the year of a Date object (from 0-99). Use getFullYear instead !! 2 3
getHours() Returns the hour of a Date object (from 0-23) 2 3
getMinutes() Returns the minute of a Date object (from 0-59) 2 3
getSeconds() Returns the second of a Date object (from 0-59) 2 3
getMilliseconds() Returns the millisecond of a Date object (from 0-999) 4 4
getTime() Returns the number of milliseconds since midnight 1/1-1970 2 3
getTimezoneOffset() Returns the time difference between the user's computer and GMT 2 3
getUTCDate() Returns the date of a Date object in universal (UTC) time 4 4
getUTCDay() Returns the day of a Date object in universal time 4 4
getUTCMonth() Returns the month of a Date object in universal time 4 4
getUTCFullYear() Returns the four-digit year of a Date object in universal time 4 4
getUTCHours() Returns the hour of a Date object in universal time 4 4
getUTCMinutes() Returns the minutes of a Date object in universal time 4 4
getUTCSeconds() Returns the seconds of a Date object in universal time 4 4
getUTCMilliseconds() Returns the milliseconds of a Date object in universal time 4 4
parse() Returns a string date value that holds the number of milliseconds since January 01 1970 00:00:00 2 3
setDate() Sets the date of the month in the Date object (from 1-31) 2 3
setFullYear() Sets the year in the Date object (four digits) 4 4
setHours() Sets the hour in the Date object (from 0-23) 2 3
setMilliseconds() Sets the millisecond in the Date object (from 0-999) 4 4
setMinutes() Set the minute in the Date object (from 0-59) 2 3
setMonth() Sets the month in the Date object (from 0-11. 0=January, 1=February) 2 3
setSeconds() Sets the second in the Date object (from 0-59) 2 3
setTime() Sets the milliseconds after 1/1-1970 2 3
setYear() Sets the year in the Date object (00-99) 2 3
setUTCDate() Sets the date in the Date object, in universal time (from 1-31) 4 4
setUTCDay() Sets the day in the Date object, in universal time (from 0-6. Sunday=0, Monday=1, etc.) 4 4
setUTCMonth() Sets the month in the Date object, in universal time (from 0-11. 0=January, 1=February) 4 4
setUTCFullYear() Sets the year in the Date object, in universal time (four digits) 4 4
setUTCHours() Sets the hour in the Date object, in universal time (from 0-23) 4 4
setUTCMinutes() Sets the minutes in the Date object, in universal time (from 0-59) 4 4
setUTCSeconds() Sets the seconds in the Date object, in universal time (from 0-59) 4 4
setUTCMilliseconds() Sets the milliseconds in the Date object, in universal time (from 0-999) 4 4
toGMTString() Converts the Date object to a string, set to GMT time zone 2 3
toLocaleString() Converts the Date object to a string, set to the current time zone 2 3
toString() Converts the Date object to a string 2 4