Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

JavaScript - The Date Object

The Date object in JavaScript is a built-in datatype that allows for the representation and manipulation of dates and times with millisecond precision. It can be created using various syntaxes and provides methods to retrieve and set date components, as well as format date strings. The Date object adheres to the ECMAScript standard, allowing for a wide range of date and time representations from 100 million days before or after January 1, 1970.

Uploaded by

ashudnahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JavaScript - The Date Object

The Date object in JavaScript is a built-in datatype that allows for the representation and manipulation of dates and times with millisecond precision. It can be created using various syntaxes and provides methods to retrieve and set date components, as well as format date strings. The Date object adheres to the ECMAScript standard, allowing for a wide range of date and time representations from 100 million days before or after January 1, 1970.

Uploaded by

ashudnahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JavaScript - The Date Object

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown
below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and
set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC
(universal, or GMT) time.

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision,
within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent
date and time till the year 275755.

Syntax
You can use any of the following syntaxes to create a Date object using Date() constructor.

new Date( )
new Date(milliseconds)
new Date(datestring)
new Date(year,month,date[,hour,minute,second,millisecond ])

Note − Parameters in the brackets are always optional.

Parameters

Here is a description of the parameters −

No Argument − With no arguments, the Date() constructor creates a Date object set to the current date and time.

milliseconds − When one numeric argument is passed, it is taken as the internal numeric representation of
the date in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates
a date that represents five seconds past midnight on 1/1/70.
datestring − When one string argument is passed, it is a string representation of a date, in the format accepted
by the Date.parse() method.
7 agruments − To use the last form of the constructor shown above. Here is a description of each argument −

year − Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you
should always specify the year in full; use 1998, rather than 98.
month − Integer value representing the month, beginning with 0 for January to 11 for December.
date − Integer value representing the day of the month.

hour − Integer value representing the hour of the day (24-hour scale).
minute − Integer value representing the minute segment of a time reading.
second − Integer value representing the second segment of a time reading.

millisecond − Integer value representing the millisecond segment of a time reading.

Return value

It returns the date string containing day, month, date, year, hours, minutes, seconds, and timezone.

Date Properties
Here is a list of the properties of the Date object along with their description.

Sr.No. Property & Description

constructor
1
Specifies the function that creates an object's prototype.

prototype
2
The prototype property allows you to add properties and methods to an object

In the following sections, we will have a few examples to demonstrate the usage of different Date properties.

Date Methods
Here is a list of the methods used with Date and their description.

Sr.No. Method & Description

Date()
1
Returns today's date and time

getDate()
2
Returns the day of the month for the specified date according to local time.

getDay()
3
Returns the day of the week for the specified date according to local time.
getFullYear()
4
Returns the year of the specified date according to local time.

getHours()
5
Returns the hour in the specified date according to local time.

getMilliseconds()
6
Returns the milliseconds in the specified date according to local time.

getMinutes()
7
Returns the minutes in the specified date according to local time.

getMonth()
8
Returns the month in the specified date according to local time.

getSeconds()
9
Returns the seconds in the specified date according to local time.

getTime()
10 Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00
UTC.

getTimezoneOffset()
11
Returns the time-zone offset in minutes for the current locale.

getUTCDate()
12
Returns the day (date) of the month in the specified date according to universal time.

getUTCDay()
13
Returns the day of the week in the specified date according to universal time.

getUTCFullYear()
14
Returns the year in the specified date according to universal time.

getUTCHours()
15
Returns the hours in the specified date according to universal time.

getUTCMilliseconds()
16
Returns the milliseconds in the specified date according to universal time.

getUTCMinutes()
17
Returns the minutes in the specified date according to universal time.

getUTCMonth()
18
Returns the month in the specified date according to universal time.
Example: Constructing a date from 7 arguments

In the example below, we have passed the year, month, date, hour, minute, second, and millisecond as a Date() constructor
argument. The Date() constructor returns the full date string, which you can see in the output.

<html>
<head>
<title> JavaScript - Date object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date(2001, 5, 14, 6, 43, 58, 342);
document.getElementById("output").innerHTML =
"The custom date is : " + date;
</script>
</body>
</html>

If we execute the above program, it returns the custom time as provided.

However, you can use the different methods of the Date object to format the date string. Let’s look at the example below.

Example: Formatting a date string

In the example below, three different methods are used to format the date string.

The toDateString() method extracts the date only from the date string and removes the time part.

The toISOString() method converts the date string into the ISO format.

The toUTCString() method converts the date string into the UTC time format.

<html>
<head>
<title> JavaScript - Formatting the date </title>
</head>
<body>
<p id = "output"> </p>
<script>
const date = new Date(999999999999);
document.getElementById("output").innerHTML +=
"The Date after 1st January, 1970 is: " + date.toDateString() + "<
"The Date after 1st January, 1970 is: " + date.toISOString() + "<b
"The Date after 1st January, 1970 is: " + date.toUTCString();
</script>
</body>
</html>

It will return the ouptut of the above provided methods, respectively.

You might also like