Javascript Date Object

The Javascript date() method of date object returns today's date and time. Date object are usually created using new Date() constructor. Use it for example to get today's date and time.

In Javascript to work with date and time, create an instance of date() object with new keyword.

There are different way to create a date object in Javascript.

  • new Date("Month dd, yyyy hh:mm:ss")
  • new Date("Month dd, yyyy")
  • new Date(yy,mm,dd,hh,mm,ss)
  • new Date(yy,mm,dd)
  • new Date(milliseconds)

Here is how you can create a Date object for each of the ways above:

Syntax

<html>
<head>
<title>Javascript Date Object</title>
var my_date=new Date("August 15, 1947 13:14:00");
var my_date=new Date("August 15, 1947");
var my_date=new Date(88,09,12,13,14,00);
var my_date=new Date(88,09,12);
var my_date=new Date(500);
</head>
<body>
</body>
</html>

Note: Paramters in the brackets are always optional

  • If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings.
  • If at least two arguments are supplied, missing arguments are either set to 1 or 0 for all others.
  • JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.
  • JavaScript Date object provides uniform behavior across platforms. The time value can be passed between systems to represent the same moment in time and if used to create a local date object, will reflect the local equivalent of the time.
  • JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.
  • Invoking JavaScript Date as a function (i.e., without the new operator) will return a string representing the current date and time.

Once created an instance of the Date object, you can access all the methods of the date object from intance variable.

Date Properties

Here is a list of each date property with description.

PropertyDescription
constructor Specifies the function that creates an object's prototype.
prototype This will allows you to add properties and function to an object.

Date Functions

Here is a list of each function of date object with description.

FunctionsDescription
Date() This will returns today's date and time
getDate() This will returns the day of the month for the specified date.
getDay() This will returns the day of the week for the specified date.
getFullYear() This will returns the year of the specified date.
getHours() This will returns the hour in the specified date.
getMilliseconds() This will returns the milliseconds in the specified date.
getMinutes() This will returns the minutes in the specified date.
getMonth() This will returns the month in the specified date.
getSeconds() This will returns the seconds in the specified date.
getTime() This will returns the numeric value of the specified date.
getTimezoneOffset() This will returns the time-zone offset in minutes for the current locale.
getUTCDate() This will returns the day of the month in the specified date according to universal time.
getUTCDay() This will returns the day of the week in the specified date according to universal time.
getUTCFullYear() This will returns the year in the specified date according to universal time.
getUTCHours() This will returns the hours in the specified date according to universal time.
getUTCMilliseconds() This will returns the milliseconds in the specified date according to universal time.
getUTCMinutes() This will returns the minutes in the specified date according to universal time.
getUTCMonth() This will returns the month in the specified date according to universal time.
getUTCSeconds() This will returns the seconds in the specified date according to universal time.
getYear() This will returns the year in the specified date according to local time.
setDate() This will sets the day of the month for a specified date.
setFullYear() This will sets the full year for a specified date.
setHours() This will sets the hours for a specified date.
setMilliseconds() This will sets the milliseconds for a specified date.
setMinutes() This will sets the minutes for a specified date.
setMonth() This will sets the month for a specified date.
setSeconds() This will sets the seconds for a specified date.
setTime() This will sets the Date object to the time represented by a number of milliseconds.
setUTCDate() This will sets the day of the month for a specified date according to universal time.
setUTCFullYear() This will sets the full year for a specified date according to universal time.
setUTCHours() This will sets the hour for a specified date according to universal time.
setUTCMilliseconds() This will sets the milliseconds for a specified date according to universal time.
setUTCMinutes() This will sets the minutes for a specified date according to universal time.
setUTCMonth() This will sets the month for a specified date according to universal time.
setUTCSeconds() This will sets the seconds for a specified date according to universal time.
setYear() This will sets the year for a specified date according to local time.
toDateString() This will returns the "date" portion of the Date as a human-readable string.
toGMTString() This will converts a date to a string, using the Internet GMT conventions.
toLocaleDateString() This will returns the "date" portion of the Date as a string, using the current locale's conventions.
toLocaleString() This will converts a date to a string, using the current locale's conventions.
toLocaleTimeString() This will returns the "time" portion of the Date as a string, using the current locale's conventions.
toString() This will returns a string representing the specified Date object.
toTimeString() Returns the "time" portion of the Date as a human-readable string.
toUTCString() This will converts a date to a string, using the universal time convention.
valueOf() This will returns the primitive value of a Date object.

Javascript Date Example

<HTML>
<TITLE>Example of Number Object</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
//Get Date example
var d = new Date();
document.write(d.getDate());
document.write(".");
document.write(d.getMonth() + 1);
document.write(".");
document.write(d.getFullYear());

document.write("</br>")

//UTC Time example
document.write(d.getUTCHours())
document.write(".")
document.write(d.getUTCMinutes() + 1)
document.write(".")
document.write(d.getUTCSeconds())

document.write("</br>")

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Above example will produce following result

//outputs of get date
25.11.2014
//outputs of UTC time
3.47.4