Date object in JavaScript
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 Descriptionconstructor
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 DescriptionDate ()
Returns the date and time of today.getDate ()
Returns the day of the month for the specified date according to Local timegetDay ()
Returns the day of the week for the specified date according to Local timegetFullYear ()
Returns the year of the given date according to Local timegetHours ()
Returns the hour of the given day according to Local timegetMilliseconds ()
Returns the millisecond of a given date according to Local timegetMinutes ()
Returns the minute of the given date according to Local timegetMonth ()
Returns the month of the given date according to Local timegetSeconds ()
Returns the second of the given date according to Local timegetTime ()
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 timegetUTCDay ()
Returns the day of the week of the given date according to Universal timegetUTCFullYear ()
Returns the year of the given date according to Universal timegetUTCHours ()
Returns the hour of the given date according to Universal timegetUTCMilliseconds ()
Returns milliseconds of the given date according to Universal timegetUTCMinutes ()
Returns the minute of the given date according to Universal timegetUTCMonth ()
Returns the month of the given date according to Universal timegetUTCSeconds ()
Returns the second of the given date according to Universal timegetYear ()
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 timesetUTCFullYear ()
Set the full year for the given date according to Universal timesetUTCHours ()
Set the time for the given date according to Universal timesetUTCMilliseconds ()
Set milliseconds for the given date according to Universal timesetUTCMinutes ()
Set minutes for the given date according to Universal timesetUTCMonth ()
Set the month for the given date according to Universal timesetUTCSeconds ()
Set the seconds for the given date according to Universal timesetYear ()
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 conventiontoLocaleFormat ()
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 DescriptionDate.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
You should read it
- Syntax of JavaScript
- Object Number in JavaScript
- What is JavaScript?
- Udemy's top 5 JavaScript courses
- String object in JavaScript
- Objects in JavaScript
- What is JavaScript? Can the Internet exist without JavaScript?
- Top site with many good JavaScript exercises to practice
- For ... loop in JavaScript
- Browser compatibility in JavaScript
- Boolean objects in JavaScript
- Learn about ES6 in Javascript
Maybe you are interested
How to view Android system update history
Driver Talent - Free Driver Updater
Intel releases new driver update package with 6GHz optimization and many other improvements
Google rolls out December Pixel update, most of it Gemini-related
Date functions in Excel, DAY, WEEKDAY, MONTH
How to update driver on Windows 10, 11