JavaScript Date object Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
JavaScriptBasicsRunning a ScriptVariablesExpressions and OperatorsObjects.Properties.MethodsJavaScript Date ObjectThe Date ObjectStringsRegular ExpressionsDefining RegExp PatternsBranches and ConditionsLoopsArraysArray MethodsSorting ArraysUser-Defined FunctionsCookiesWindowsFrames

Home > JavaScript > The Date Objectprinter 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 - JavaScript Objects, Properties and Methods Previous - Objects, Properties and Methods     Next - Strings Next - JavaScript Strings


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript