Category: javascript
date and methods in java
Published on 09 Mar 2026
Explanation
In JavaScript,
the Date object is used
to work with dates and times.
It allows you to
1.create,
2.format
3.manipulate
4.date and time values.
If no argument is passed, it returns the current date
and time.
Code:
const now = new Date(); console.log(now);
Explanation
getFullYear()
method returns the full year (4 digits)
getMonth()
returns the month (0-11).
Note: January is 0 and December is 11.
Code:
const date = new Date(); console.log(date.getFullYear()); console.log(date.getMonth());
Explanation
getDate()
method returns the day of the month (1-31).
getDay()
method returns the day of the week (0-6).
Sunday is 0.
Code:
const date = new Date(); console.log(date.getDate()); console.log(date.getDay());
Explanation
getHours()
method returns the hour (0-23).
getMinutes()
method returns the minutes (0-59).
Code:
const date = new Date(); console.log(date.getHours()); console.log(date.getMinutes());
Explanation
getSeconds()
method returns the seconds (0-59).
setFullYear()
method sets the year of a Date object.
Code:
const date = new Date(); console.log(date.getSeconds()); date.setFullYear(2030);
Explanation
toDateString()
converts Date object into a readable string.
toLocaleDateString()
converts date as a string using local
conventions.
Code:
const date = new Date(); console.log(date.toDateString()); console.log(date.toLocaleDateString());