The Date object in JavaScript allows you to interact with dates and times, such as days, months, years, hours, minutes, seconds, and milliseconds. To acquire a Date for the current time or Date, use new Date(). The time is maintained internally as a number of milliseconds since January 1, 1970 UTC. Lets take an example to fetch current date and time.
const x = new Date();
let val = x.getDate();
console.log(val); // 18
Method | Description | Example |
---|---|---|
getDate() | The day of the month is returned. |
|
getDay() | The day of the week is returned. |
|
getFullYear() | Returns the year |
|
getHours() | Returns the hour |
|
getMinutes() | Returns the minute |
|
getSeconds() | Returns the seconds |
|
getMilliseconds() | Returns the milliseconds |
|
setTime() | Sets a date to a number of milliseconds |
|
setDate() | Sets a date |
|
toString() | This function converts a Date object to a string. |
|
parse() | Returns the value of milliseconds since January 1, 1970 after parsing a date string. |
|