Javascript Dates reference

Javascript build-in Date object reference.

Create dates:

To create an object: new Date();
new Date(milliseconds);
new Date(datestring);
new Date(year, month, day, hours, minutes, seconds, ms);
A Date object in JavaScript is created with the new operator and the Date() constructor in 4 possible ways:
  1. When you create a Date object with the constructor without arguments you get an object with the current date and time.
  2. When you use a numeric argument, you will receive a date object containing a date and time which differs in (+/-) milliseconds compared to 01/01/1970.
  3. You got a date object when one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse() method.
  4. You can create a date object specifying seven numeric arguments that specify the individual fields of the date and time. The five last arguments are optional.
To use as a function: Date([param])
To use the constructor function, Date, as an function, then the function will return a string representation of the current date and time. In this case will all arguments be ignored.
An example of using the new Date([param]):
<script type="text/javascript">
var datetime = new Date()
// The date object contains: 'current date and time'
document.write("The date object contains: "+datetime+"<br>");
datetime = new Date(42*365*24*60*60*1000);
// The date object contains: Thu Dec 22 2011 01:00:00 GMT+0100
document.write("The date object contains: "+datetime+"<br>");
datetime = new Date("01/02/2012");
// The date object contains: Mon Jan 02 2012 00:00:00 GMT+0100
document.write("The date object contains: "+datetime+"<br>");
datetime = new Date(2012,7); // 0 = january, 1 = february, ...
// The date object contains: Wed Aug 01 2012 00:00:00 GMT+0200
document.write("The date object contains: "+datetime+"<br>");
</script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Prototype Methods:

Syntax: getTime()
Converts date and time in a date object to a single integer that is a millisecond representation of the date object.

Millisecond representation of a date is independent of the time zone.

Return value: The millisecond representation of the adjusted Date object.
An example of using the getTime() :
<script type="text/javascript">
  datetime = new Date();
   document.write("Date Today (locale): "
    +datetime.toLocaleString()+"<br>");
  document.write("Date Today using getTime() : "
    +datetime.getTime()+" milliseconds<br>");
</script>

Constructor (Date) Methods:

Syntax: Date.parse(string)
Parses the date contained in this string and returns it in millisecond format, which can be used directly to create a new Date object or set a date with the setTime() method.
Return value: Milliseconds between the specified date and time and midnight GMT on January 1, 1970.
string: A string containing the date and time to be parsed.
An example of using the parse() :
<script type="text/javascript">
  datetime = new Date();
  document.write("Date Today (locale): "
    +datetime.toLocaleString()+"<br>");
  document.write("Date Today using Date.parse() : "
    +datetime.setTime(Date.parse("2/2/2012"))+" milliseconds<br>");
  document.write("New date (locale): "
    +datetime.toLocaleString()+"<br>");
</script>

Prototype Properties:

Syntax: Date.prototype.constructor
This property of the Date.prototype holds the reference back to the Date object.
Important note: This is the same as the constructor property of an instantiated object

Constructor (Date) Properties:

Syntax: Date.constructor
The constructor property is a reference to the function that will be invoked to create a Date object.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.