Hackforge Academy

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());

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community