Table of Contents
This guide covers date object in JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.