Home >
JavaScript > The Date Object
printer version
The Date Object
JavaScript provides an object representing date and time that includes methods and functions to manipulate these.
To create a Date object the
Date() constructor along with the
new operator is used.
The object's initial value is the current date and time on the user's machine.
Creating a Date Object
<script language="javascript" type="text/javascript">
var present = new Date();
</script>
The Date object supports a number of methods that allow you to get or set year, month, day, hours, minutes or seconds.
Reading the Date Object
<script language="javascript" type="text/javascript">
var present = new Date();
// This prints out the year in four-digit form
document.write(present.getFullYear());
/* This prints out the month in one-digit or two-digit form
where January=0 etc */
document.write(present.getMonth());
// This prints out the day as a digit where Sunday=0 etc.
document.write(present.getDay());
// This prints out hours in 24 hour format.
document.write(present.getHours());
// This prints out minutes in one-digit or two-digit form
document.write(present.getMinutes());
// This prints out seconds in one-digit or two-digit form
document.write(present.getSeconds());
</script>
Setting the Date Object
<script language="javascript" type="text/javascript">
var present = new Date();
/* This will set the date and time to
1 second before midnight on 31st December 1980 */
present.setFullYear(1980);
present.setMonth(11);
present.setDate(3);
present.setHours(23);
present.setMinutes(59);
present.setSeconds(59);
</script>
Previous - Objects, Properties and Methods
Next - Strings
Privacy | Terms | Contact | Links | Sitemap | RSS Feeds 
©2010 www.webdesignworkmate.co.uk all rights reserved
Design and Production by smallbizonline website design © 2000-2010