Open Directory Site JavaScript Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> JavaScript >> JavaScript Date Object

 JavaScript Date Object

 

The Date object

The Date object is one of JavaScripts built-in objects. It is used to return a date. To do this you must declare a variable:

mydate=new Date()

You can write a date inside the parentheses, if not, the "mydate" variable contains today's date, see the Date() example below. Now you can access all the methods in the Date object from this variable. Example:

mydate.getDay()

The most common methods

Methods Explanation NN IE ECMA

Date() Returns a new Date object 2.0 3.0 1.0

getDate() Returns the date from a Date object. (1-31) 2.0 3.0 1.0

getDay() Returns the weekday. (0-6) 2.0 3.0 1.0

getMonth() Returns the month. (0-11) 2.0 3.0 1.0

getFullYear() Returns the year. (2000) 4.0 4.0 1.0

getHours() Returns the hour as a value between 0 and 23 2.0 3.0 1.0

getMinutes() Returns the minute. (0-59) 2.0 3.0 1.0

getSeconds() Returns the second. (0-59) 2.0 3.0 1.0

Example

1.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.

Coding

<html>

<body>

<script language="JavaScript">

var d = new Date()

document.write(d.getDate())

document.write(".")

document.write(d.getMonth() + 1)

document.write(".")

document.write(d.getFullYear())

</script>

</body>

</html>

Output

9.8.2000

2. Time()

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

Coding

<html>

<body>

<script language="JavaScript">

var d = new Date()

document.write(d.getHours())

document.write(".")

document.write(d.getMinutes())

document.write(".")

document.write(d.getSeconds())

</script>

</body>

</html>

Output

14.39.19

3. 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.

Coding

<html>

<body>

<script language="JavaScript">

var d = new Date()

d.setFullYear("1990")

document.write(d)

</script>

</body>

</html>

Output

Thu Aug 9 14:40:16 UTC+0530 1990

4. UTC Time

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

Coding

<html>

<body>

<script language="JavaScript">

var d = new Date()

document.write(d.getUTCHours())

document.write(".")

document.write(d.getUTCMinutes())

document.write(".")

document.write(d.getUTCSeconds())

</script>

</body>

</html>

Output

9.11.52

5. 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.

Coding

<html>

<body>

<script language="JavaScript">

var d=new Date()

var weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")

document.write("Today is " + weekday[d.getDay()])

</script>

</body>

</html>

Output

Today is Wednesday

6. 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.

Coding

<html>

<head>

<script Language="JavaScript">

var timer = null

function stop()

{

clearTimeout(timer)

}

function start()

{

var time = new Date()

var hours = time.getHours()

var minutes = time.getMinutes()

var seconds = time.getSeconds()

var clock = hours

clock += ((minutes < 10) ? ":0" : ":") + minutes

clock += ((seconds < 10) ? ":0" : ":") + seconds

document.forms[0].display.value = clock;

timer = setTimeout("start()",1000);

}

</script>

</head>

<body onload="start()" onunload="stop()">

<form>

<input type="text" name="display">

</form>

</body>

</html>

Output

Displays system running time

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting