JavaScript Date
JavaScript Date
object is used for dealing with times and dates. You can form a new Date object that
signifies the present date and time, or it could be some fixed date and time.
Example:
let currentDate
=
new
Date();
console.log(currentDate);
Output:
JavaScript Date Methods
There are various methods available in JavaScript Date object.
Method | Description |
---|---|
getDate() | Returns the day of the month (1-31) based on local time |
getFullYear() | Retrieves the year based on local time |
getMonth() | Returns the month, in the range 0 through 11, based on local time |
setDate() | Sets the day of the month based on local time |
setFullYear() | Sets the entire year based on local time |
setMonth() | Sets the month based on local time |
Example:
let date
=
new
Date();
let day
= date.getDate();
console.log(day);
let date
=
new
Date();
let year
= date.getFullYear();
console.log(year);
let date
=
new
Date();
let month
= date.getMonth()+1;
console.log(month);
let date
=
new
Date();
date.setDate(26);
let D
= date.getDate();
console.log(D);
Output:
25
2024
3
26