The Date
object is used to work with dates and times.
Examples
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.
The Date
object
The Date object is used to work
with dates and times.
You create an instance of the
Date object with the "new" keyword.
To store the current date in a
variable called "my_date":
After creating an instance of
the Date object, you can access all the methods of the object
from the "my_date" variable. If, for example, you want to return
the date (from 1-31) of a Date object, you should write the
following:
You can also write a date
inside the parentheses of the Date() object, like this:
new Date("Month dd, yyyy hh:mm:ss")
new Date("Month dd, yyyy")
new Date(yy,mm,dd,hh,mm,ss)
new Date(yy,mm,dd)
new Date(milliseconds)
|
Here is how you can create a
Date object for each of the ways above:
var my_date=new Date("October 12, 1988 13:14:00")
var my_date=new Date("October 12, 1988")
var my_date=new Date(88,09,12,13,14,00)
var my_date=new Date(88,09,12)
var my_date=new Date(500)
|
The Most
Common Methods
NN: Netscape, IE:
Internet Explorer, ECMA: Web Standard
Methods
|
Explanation
|
NN
|
IE
|
ECMA
|
| Date() |
Returns a
Date object |
2.0 |
3.0 |
1.0 |
| getDate() |
Returns
the date of a Date object (from 1-31) |
2.0 |
3.0 |
1.0 |
| getDay() |
Returns
the day of a Date object (from 0-6. 0=Sunday, 1=Monday,
etc.) |
2.0 |
3.0 |
1.0 |
|
getMonth() |
Returns
the month of a Date object (from 0-11. 0=January,
1=February, etc.) |
2.0 |
3.0 |
1.0 |
|
getFullYear() |
Returns
the year of the Date object (four digits) |
4.0 |
4.0 |
1.0 |
|
getHours() |
Returns
the hour of the Date object (from 0-23) |
2.0 |
3.0 |
1.0 |
|
getMinutes() |
Returns
the minute of the Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
|
getSeconds() |
Returns
the second of the Date object (from 0-59) |
2.0 |
3.0 |
1.0 |
The
built-in Math object includes mathematical constants and
functions.
The Math
object
The built-in Math object
includes mathematical constants and functions. You do not need
to create the Math object before using it.
To store a random number
between 0 and 1 in a variable called "r_number":
To store the rounded number of
8.6 in a variable called "r_number":
The Most
Common Methods
NN: Netscape, IE:
Internet Explorer, ECMA: Web Standard
Methods
|
Explanation
|
NN
|
IE
|
ECMA
|
|
max(x,y) |
Returns the number with
the highest value of x and y |
2.0 |
3.0 |
1.0 |
|
min(x,y) |
Returns the number with
the lowest value of x and y |
2.0 |
3.0 |
1.0 |
|
random() |
Returns a random number
between 0 and 1 |
2.0 |
3.0 |
1.0 |
|
round(x) |
Rounds x to the nearest
integer |
2.0 |
3.0 |
1.0 |
Page 1
2 3
4 |
|