Date

Contact Us or call 1-877-932-8228
Date

Date

The Date object has methods for manipulating dates and times. JavaScript stores dates as the number of milliseconds since January 1, 1970. The sample below shows the different methods of creating date objects, all of which involve passing arguments to the Date() constructor.

Code Sample:

BuiltInObjects/Demos/DateObject.html
---- C O D E   O M I T T E D ----

<body>
<h1>Date Object</h1>
<h2>New Date object with current date and time</h2>
<pre>
//Syntax: new Date();
var now = new Date();
</pre>
<strong>Result:</strong>
<script>
	var now = new Date();
	document.write(now);
</script>

<h2>New Date object with specific date and time</h2>
<pre>
//Syntax: new Date("month dd, yyyy hh:mm:ss);
var redSoxWin = new Date("October 21, 2004 12:01:00");
</pre>
<strong>Result:</strong>
<script>
	var redSoxWin = new Date("October 21, 2004 12:01:00");
	document.write(redSoxWin);
</script>

<pre>
//Syntax: new Date(year, month, day, hours, min, sec, millisec);
redSoxWin = new Date(2004, 9, 21, 12, 01, 00, 00);
</pre>
<strong>Result:</strong>
<script>
	redSoxWin = new Date(2004, 9, 21, 12, 01, 00, 00);
	document.write(redSoxWin);
</script>
</body>
</html>

Code Explanation

This page is shown in a browser below.

A few things to note:

  1. To create a Date object containing the current date and time, the Date() constructor takes no arguments.
  2. When passing the date as a string to the Date() constructor, the time portion is optional. If it is not included, it defaults to 00:00:00. Also, other date formats are acceptable (e.g., "3/2/2017" and "03-02-2017").
  3. When passing date parts to the Date() constructor, dd, hh, mm, ss, and ms are all optional. The default for dd is 1; the other parameters default to 0.
  4. Months are numbered from 0 (January) to 11 (December). In the example above, 9 represents October.

Some common date methods are shown below. In all the examples, the variable rightNow contains "Thu Mar 02 00:23:54:650 EDT 2017".

Common Date Methods
Method Description
getDate() Returns the day of the month (1-31).
rightNow.getDate();
//Returns 2
getDay() Returns the day of the week as a number (0-6, 0=Sunday, 6=Saturday).
rightNow.getDay();
//Returns 4
getMonth() Returns the month as a number (0-11, 0=January, 11=December).
rightNow.getMonth();
//Returns 2
getFullYear() Returns the four-digit year.
rightNow.getFullYear();
//Returns 2017
getHours() Returns the hour (0-23).
rightNow.getHours();
//Returns 0
getMinutes() Returns the minute (0-59).
rightNow.getMinutes();
//Returns 23
getSeconds() Returns the second (0-59).
rightNow.getSeconds();
//Returns 54
getMilliseconds() Returns the millisecond (0-999).
rightNow.getMilliseconds();
//Returns 650
getTime() Returns the number of milliseconds since midnight January 1, 1970.
rightNow.getTime();
//Returns 1488432234650
getTimezoneOffset() Returns the time difference in minutes between the user's computer and GMT.
rightNow.getTimezoneOffset();
//Returns 300
toLocaleString() Returns the Date object as a string.
rightNow.toLocaleString();
//Returns 3/2/2017, 12:23:54 AM
toGMTString() Returns the Date object as a string in GMT timezone.
rightNow.toGMTString();
//Returns Thu, 02 Mar 2017 05:44:30 GMT

You can see these examples in a browser by opening BuiltInObjects/Demos/DateMethods.html.

Next