The "Date" Object Type - Managing Dates and Times

This section provides a quick description and a tutorial example script on the 'Date' built-in object type, which is used to create 'Date' objects.

The "Date" object type is a built-in object type to be used to create a "Date" object, representing a moment in time. It has the following main features:

Here is a tutorial example script showing some of the those features:

// Date_Object_Type.js
// Copyright (c) 2013 by Dr. Herong Yang, herongyang.com

   // Creating a new Date object
   var myObject = new Date(2008, 0, 1, 2, 3, 4);

   // Checking this object
   println("\nAbout this \"myObject\":");
   println("   Type = "+(typeof myObject));
   println("   Instance Of Object: "+(myObject instanceof Object));
   println("   Instance Of Date: "+(myObject instanceof Date));

   // Using Number's inherited methods
   var v = myObject.valueOf();
   var s = myObject.toString();
   var h = myObject.getHours();

   println("\nExecution result:");
   println("   typeof myObject = "+(typeof myObject));
   println("   typeof myObject.valueOf() = "+(typeof v));
   println("   typeof myObject.toString() = "+(typeof s));
   println("   typeof myObject.getHours(() = "+(typeof h));
   println("   value of myObject.valueOf() = "+v);
   println("   value of myObject.toString() = "+s);
   println("   value of myObject.toString() = "+h);

   println("\nUsing methods of the Date constructor:");
   var myTime = Date.parse("Tue Jan 01 2008");
   var myDate = new Date(myTime);
   println("   typeof myTime = "+(typeof myTime));
   println("   typeof myDate = "+(typeof myDate));
   println("   myTime = "+myTime);
   println("   myDate.toString() = "+myDate.toString());

If you run this script with "jrunscript", you will get:

About this "myObject":
   Type = object
   Instance Of Object: true
   Instance Of Date: true

Execution result:
   typeof myObject = object
   typeof myObject.valueOf() = number
   typeof myObject.toString() = string
   typeof myObject.getHours(() = number
   value of myObject.valueOf() = 1199170984000
   value of myObject.toString() = Tue Jan 01 2008 02:03:04 GMT-0000 
   value of myObject.toString() = 2

Using methods of the Date constructor:
   typeof myTime = number
   typeof myDate = object
   myTime = 1199163600000
   myDate.toString() = Tue Jan 01 2008 00:00:00 GMT-0000 

Now we know how to create "Date" objects with the "Date()" constructor.

Read other "Date" related chapters to see more tutorial examples.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

Introduction to Built-in Object Types

 Overview of Built-in Object Types

 The "Object" Object Type - The Root Object Type

 The "Global" Object Type - The Invisible Global Container

 Global Properties and Functions Defined in ECMAScript

 Global Properties and Functions Provided by "jrunscript"

 The "Function" Object Type - Functions Are Objects

 The "Array" Object Type - Arrays Are Objects

 The "String" Object Type - Not Equal to String Primitive Type

 The "Boolean" Object Type - Wrapping Boolean Values into Objects

 The "Number" Object Type - Not Equal to Number Primitive Type

The "Date" Object Type - Managing Dates and Times

 The "RegExp" Object Type - Regular Expression Patterns

 The "Error" Object Type - Runtime Exceptions

 The "Math" Object Type - The Math Container

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version