Date object in JavaScript

Date object is a data type built into JavaScript Language.

Date object is a data type built into JavaScript Language. Date objects are created with new Date () as below:

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

The ECMAScript standard requires that the Date object can represent any date and time, accurate to milliseconds, within 100 million days before or after January 1, 1970. This is a sequence of plus or minus 273,785 years, so JavaScript can represent the date and time until 275755.

Syntax

You can use the following syntax to create a Date object using the Date () constructor.

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

Note - Parameters in parentheses are always optional.

The following is a description of the parameters according to each syntax:

No parameters - With no parameters, the Date () constructor creates a Date object set to the current time and date.

milliseconds - When a numeric parameter is passed, it receives the representation of the internal numerical value of the day in milliseconds, when returned by the getTime () method. For example, passing parameter 5000 creates a day that represents 5 seconds before midnight on January 1, 1970.

datestring - When a string parameter is passed, it is a string representation of a date, in a format accepted by the Date.parse () method .

7 agruments - To use the last form of the constructor shown above. Here we describe each parameter one:

year - Integer value representing year. For compatibility (avoid the Y2K problem), you should always specify the full year, using 1998 instead of 98.

month - Integer value representing the month, starting with 0 for January and 11 for December.

date - Integer value representing the day of the month.

hour - Integer value representing the time of day (24 hours).

minute - Integer value representing minute.

second - Integer value represents the second.

millisecond - Integer value representing milliseconds.

Properties of Date

Below is a list of the properties of the Date object along with their description:

Properties Description

constructor

Define the function that creates an object prototype.

prototype

This property allows you to add properties and methods to an object.

Methods of Date

Below is a list of methods of the Date object along with their description:

Method Description

Date ()

Returns the date and time of today.

getDate ()

Returns the day of the month for the specified date according to Local time

getDay ()

Returns the day of the week for the specified date according to Local time

getFullYear ()

Returns the year of the given date according to Local time

getHours ()

Returns the hour of the given day according to Local time

getMilliseconds ()

Returns the millisecond of a given date according to Local time

getMinutes ()

Returns the minute of the given date according to Local time

getMonth ()

Returns the month of the given date according to Local time

getSeconds ()

Returns the second of the given date according to Local time

getTime ()

Returns the numerical value of the given day when the number of milliseconds is from January 1970, 00:00:00 UTC.

getTimezoneOffset ()

Returns the Time-zone Offset in minutes for the current Locale.

getUTCDate ()

Returns the day of the month of the given date according to Universal time

getUTCDay ()

Returns the day of the week of the given date according to Universal time

getUTCFullYear ()

Returns the year of the given date according to Universal time

getUTCHours ()

Returns the hour of the given date according to Universal time

getUTCMilliseconds ()

Returns milliseconds of the given date according to Universal time

getUTCMinutes ()

Returns the minute of the given date according to Universal time

getUTCMonth ()

Returns the month of the given date according to Universal time

getUTCSeconds ()

Returns the second of the given date according to Universal time

getYear ()

Old method - Returns the year of the given date according to Local time. You use getFullYear instead.

setDate ()

Returns the date of the month for the specified date according to Local time.

setFullYear ()

Set the full year for the given date according to Local time.

setHours ()

Set the time for the given day according to Local time.

setMilliseconds ()

Set milliseconds for a given date according to Local time.

setMinutes ()

Set the minutes for the given day according to Local time.

setMonth ()

Set the month for the given day according to Local time.

setSeconds ()

Set the seconds for the given day according to Local time.

setTime ()

Set the time the Date object is represented by the number of milliseconds from January, 1970, 00:00:00 UTC.

setUTCDate ()

Set the date of the month for the given date according to Universal time

setUTCFullYear ()

Set the full year for the given date according to Universal time

setUTCHours ()

Set the time for the given date according to Universal time

setUTCMilliseconds ()

Set milliseconds for the given date according to Universal time

setUTCMinutes ()

Set minutes for the given date according to Universal time

setUTCMonth ()

Set the month for the given date according to Universal time

setUTCSeconds ()

Set the seconds for the given date according to Universal time

setYear ()

Old method - Set the year for the given date according to Local time. You use setFullYear instead.

toDateString ()

Returns a series of days that humans read.

toGMTString ()

Old method - Convert 1 day to 1 string using Internet GMT conventions. You use toUTCString instead.

toLocaleDateString ()

Returns the date as a string, using the current Locale convention

toLocaleFormat ()

Transform date into string, using format string.

toLocaleString ()

Transform day into string, using current Locale conventions.

toLocaleTimeString ()

Returns the time of a day in string form, using the current Locale convention.

toSource ()

Returns a string representing the source for an equivalent Date object, you can use this value to create a new object.

toString ()

Returns a string representing the given Date object.

toTimeString ()

Returns the time of the Date object in human readable form.

toUTCString ()

Transform a day into a string, using Universal time convention.

valueOf ()

Returns the initial value of a Date object.

Static methods (Static Method) of Date

In addition to the methods listed above, the Date object also defines two static methods (Static Method). These methods are called via the Date () constructor itself.

Method Description

Date.parse ()

Analyze a string representation of a date and time and return the representation of the internal millisecond of that day.

Date.UTC ()

Returns the millisecond representation of the given date and time UTC.

According to Tutorialspoint

Previous article: Array (Array) in JavaScript

Next lesson: Math object in JavaScript

Thanks for reading the post

Date object in JavaScript

Please share this article to your friends
« Math object in JavaScript
Array (Array) in JavaScript »