JavaScript Date object is used to represent a moment in time It is used to work with date and time This time value is since 1 January 1970 UTC (Coordinated Universal Time).
Syntax:
new Date();
Example: If nothing as the parameter is given, it returns the present date and time.
Javascript
// If nothing as a parameter is given, // it represents the present date and time. let A = new Date(); // Printing present date and time. console.log(A); |
Output:
Mon Nov 14 2022 16:56:38 GMT+0530 (India Standard Time)
The complete list of JavaScript Date methods and properties are listed below:
JavaScript Date Constructor: A constructor gets called when an object is created using the new keyword.
Constructor | Description | Examples |
---|---|---|
Date() | Create a Date instance or return a string representing the current time. |
JavaScript Date Properties: A JavaScript property is a member of an object that associates a key with a value.
- Instance Properties: An instance property is a property that has a new copy for every new instance of the class.
Properties | Description | Examples |
---|---|---|
constructor | Returns the constructor function for an object. |
JavaScript Date Methods: JavaScript methods are actions that can be performed on objects.
- Static Method: If the method is called using the Date class itself then it is called a static method.
Static Methods | Description | Examples |
---|---|---|
now() | Return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. | |
parse() | Return the time difference in milliseconds from, January 1, 1970, till the date we provide. | |
UTC() | Return the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. |
- Instance Method: If the method is called on an instance of a date object then it is called an instance method.
Instance Methods | Description | Examples |
---|---|---|
getDate() | Fetch the date of a month from a given Date object. | |
getDay() | Fetch the day of a week(0 to 6) from a given Date object. | |
getFullYear() | Fetch the year from a given Date object. | |
getHours() | Return the hours from a given Date object. | |
getMilliseconds() | Fetch the milliseconds from a given Date object. | |
getMinutes() | Fetch the minutes from the given Date object. | |
getMonth() | Fetch the month(0 to 11) from the given Date object. | |
getSeconds() | Fetch the seconds from the given Date object. | |
getTime() | Return the number of milliseconds since 1 January 1970 . | |
getTimezoneOffset() | Return the time difference between in minutes. | |
getUTCDate() | Fetch the date of a month according to universal time from a given Date object. | |
getUTCDay() | Fetch the date of a month according to universal time from a given Date object. | |
getUTCFullYear() | Fetch the year according to universal time from a given Date object. | |
getUTCHours() | Fetch the hours according to universal time from a given Date object. | |
getUTCMilliseconds() | Fetch the millisecond according to universal time from a given Date object. | |
getUTCMinutes() | Fetch the minutes according to universal time from a given Date object. | |
getUTCMonth() | Fetch the month according to universal time from a given Date object. | |
getUTCSeconds() | Fetch the second according to universal time from a given Date object | |
getYear() | Get the year on a specified date according to universal time. | |
setDate() | Set the date of a month into a date object which is created using date() constructor. | |
setFullYear() | Set year into a date object which is created using Date() constructor. | |
setHours() | Set hours into a date object which is created using the Date() constructor. | |
setMilliseconds() | Set milliseconds into a date object which are created using date() constructor. | |
setMinutes() | Set minutes into a Date object which is created using Date() constructor. | |
setMonth() | Set month into a date object which is created using the Date() constructor. | |
setSeconds() | Set seconds into a Date object which is created using Date() constructor. | |
setUTCDate() | Set date of a month according to universal time into a date object. | |
setUTCFullYear() | Set year into a date object according to universal time. | |
setUTCHours() | Set hours into a date object according to universal time. | |
setUTCMilliseconds() | Set milliseconds according to universal time into a date object. | |
setUTCMinutes() | Set minutes according to universal time into a date object. | |
setUTCMonth() | Set month according to universal time into a date object. | |
setUTCSeconds() | Set seconds according to universal time into a date object. | |
setYear() | Set the year in a specified date according to universal time. | |
toDateString() | Convert the given date object’s contents of the date portion into a string. | |
toISOString() | Convert the given date object’s contents into a string in ISO format (ISO 8601). | |
toJSON() | Convert the given date object’s contents into a string. | |
toLocaleDateString() | Convert a date to a string. | |
toLocaleTimeString() | Fetch the time from a given Date object. | |
toLocaleString() | Convert a date and time to a string. | |
toString() | Convert the given date object’s contents into a string. | |
toTimeString() | Return the time portion of the given date object in English. | |
toUTCString() | Convert the given date object’s contents into a string according to the universal time zone UTC. | |
valueOf() | Get the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date. |