Get and Set methods with Date Objects in Javascript

Suneet Agrawal
2 min readNov 12, 2024

--

Working with dates is a common task in web development, and JavaScript provides a built-in Date object to handle date and time-related operations. Two essential methods for manipulating Date objects are get and set methods. In this blog, we will explore how to use these methods to retrieve and modify various components of a Date object.

This post was originally posted at https://agrawalsuneet.github.io/blogs/get-and-set-methods-with-date-objects-in-javascript/ and later reposted on Medium.

The Date Object

Before we delve into the get and set methods, let’s briefly revisit the Date object itself. You can create a new Date object in JavaScript as follows:

const currentDate = new Date();

This creates a Date object representing the current date and time. Now, let’s explore the get and set methods for working with Date objects.

Get Methods

Getting the Year

To retrieve the year from a Date object, you can use the getFullYear() method:

const currentYear = currentDate.getFullYear();
console.log("Current year: " + currentYear);

Getting the Month

To get the month (0–11) from a Date object, you can use the getMonth() method:

const currentMonth = currentDate.getMonth();
console.log("Current month: " + currentMonth);

Getting the Day of the Month

To get the day of the week (0–6, where 0 is Sunday and 6 is Saturday), you can use the getDay() method:

Please continue reading at https://agrawalsuneet.github.io/blogs/get-and-set-methods-with-date-objects-in-javascript/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

No responses yet