Date and Time are something almost all programmers have to deal with while working with almost all kinds of technologies. Whether it is related to web technology or any application the programmers need to know how to estimate times. In this article, we are going to learn how to get all the dates in a month using javascript.
How to Get All Dates in a Month Using JavaScript
- Create a function that accepts the current year and the month. This is important because depending on the current year the function will determine whether it is a leap year or not.
- Then we need to create a constructor in the function and get the current date. We need to pass the year and month parameters. Additionally, we need to pass another argument to the function which is 1. This describes that the first date of the month is set to 1.
- Then we need to create a loop in which we will keep pushing the data until the dates are out of the current month.
Example
function get_all_dates(year, month) {
let date = new Date(year, month, 1);
let dates = [];
let i = 0;
while (date.getMonth() === month) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
console.log(dates[i]);
i++;
}
}
let curr_date = new Date();
console.log("For this month:");
console.log("\n");
get_all_dates(curr_date.getFullYear(), curr_date.getMonth());
console.log("\n");
console.log("For the month of Jan 2019:");
console.log("\n");
let date = new Date('2019-01-27');
get_all_dates(date.getFullYear(), date.getMonth());
Output
2022-08-31T18:30:00.000Z
2022-09-01T18:30:00.000Z
2022-09-02T18:30:00.000Z
2022-09-03T18:30:00.000Z
2022-09-04T18:30:00.000Z
2022-09-05T18:30:00.000Z
2022-09-06T18:30:00.000Z
2022-09-07T18:30:00.000Z
2022-09-08T18:30:00.000Z
2022-09-09T18:30:00.000Z
2022-09-10T18:30:00.000Z
2022-09-11T18:30:00.000Z
2022-09-12T18:30:00.000Z
2022-09-13T18:30:00.000Z
2022-09-14T18:30:00.000Z
2022-09-15T18:30:00.000Z
2022-09-16T18:30:00.000Z
2022-09-17T18:30:00.000Z
2022-09-18T18:30:00.000Z
2022-09-19T18:30:00.000Z
2022-09-20T18:30:00.000Z
2022-09-21T18:30:00.000Z
2022-09-22T18:30:00.000Z
2022-09-23T18:30:00.000Z
2022-09-24T18:30:00.000Z
2022-09-25T18:30:00.000Z
2022-09-26T18:30:00.000Z
2022-09-27T18:30:00.000Z
2022-09-28T18:30:00.000Z
2022-09-29T18:30:00.000Z
For the month of Jan 2019:
2018-12-31T18:30:00.000Z
2019-01-01T18:30:00.000Z
2019-01-02T18:30:00.000Z
2019-01-03T18:30:00.000Z
2019-01-04T18:30:00.000Z
2019-01-05T18:30:00.000Z
2019-01-06T18:30:00.000Z
2019-01-07T18:30:00.000Z
2019-01-08T18:30:00.000Z
2019-01-09T18:30:00.000Z
2019-01-10T18:30:00.000Z
2019-01-11T18:30:00.000Z
2019-01-12T18:30:00.000Z
2019-01-13T18:30:00.000Z
2019-01-14T18:30:00.000Z
2019-01-15T18:30:00.000Z
2019-01-16T18:30:00.000Z
2019-01-17T18:30:00.000Z
2019-01-18T18:30:00.000Z
2019-01-19T18:30:00.000Z
2019-01-20T18:30:00.000Z
2019-01-21T18:30:00.000Z
2019-01-22T18:30:00.000Z
2019-01-23T18:30:00.000Z
2019-01-24T18:30:00.000Z
2019-01-25T18:30:00.000Z
2019-01-26T18:30:00.000Z
2019-01-27T18:30:00.000Z
2019-01-28T18:30:00.000Z
2019-01-29T18:30:00.000Z
2019-01-30T18:30:00.000Z
Explanation
In the above code we have done the following:
- We created a user-defined function in the code to get all the dates of any month.
- We named the function get_all_dates. We are accepting two parameters namely the year and the month.
- We then initialized a variable using the constructor Date() and passed the year and month argument. We have passed another argument 1 to initialize the date of the month.
- Now we created an empty dynamic array named dates.
- We run a while loop until the date variable gets a date that is no more present in the current month. Until the loop runs, we will keep pushing the dates into the date array and set the date using the setDate() function.
- getFullYear method returns the four-digit year of the given date and getMonth method returns the zero-based value of the month(January = 0, December = 11).
However, we may want the dates to be in the form of strings. This can be quickly done through the toString() method in javascript.
Example
function get_all_dates(year, month) {
let date = new Date(year, month, 1);
let dates = [];
let i=0;
while (date.getMonth() === month) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
console.log(dates[i].toString());
i=i+1;
}
}
let curr_date = new Date();
let date = new Date('2004-02-27');
console.log("For this month:");
console.log("\n");
get_all_dates(curr_date.getFullYear(), curr_date.getMonth());
console.log("\n");
console.log("For the month of Feb 2004:");
console.log("\n");
get_all_dates(date.getFullYear(), date.getMonth());
Output
For this month:
Thu Sep 01 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Sep 02 2022 00:00:00 GMT+0530 (India Standard Time)
Sat Sep 03 2022 00:00:00 GMT+0530 (India Standard Time)
Sun Sep 04 2022 00:00:00 GMT+0530 (India Standard Time)
Mon Sep 05 2022 00:00:00 GMT+0530 (India Standard Time)
Tue Sep 06 2022 00:00:00 GMT+0530 (India Standard Time)
Wed Sep 07 2022 00:00:00 GMT+0530 (India Standard Time)
Thu Sep 08 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Sep 09 2022 00:00:00 GMT+0530 (India Standard Time)
Sat Sep 10 2022 00:00:00 GMT+0530 (India Standard Time)
Sun Sep 11 2022 00:00:00 GMT+0530 (India Standard Time)
Mon Sep 12 2022 00:00:00 GMT+0530 (India Standard Time)
Tue Sep 13 2022 00:00:00 GMT+0530 (India Standard Time)
Wed Sep 14 2022 00:00:00 GMT+0530 (India Standard Time)
Thu Sep 15 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Sep 16 2022 00:00:00 GMT+0530 (India Standard Time)
Sat Sep 17 2022 00:00:00 GMT+0530 (India Standard Time)
Sun Sep 18 2022 00:00:00 GMT+0530 (India Standard Time)
Mon Sep 19 2022 00:00:00 GMT+0530 (India Standard Time)
Tue Sep 20 2022 00:00:00 GMT+0530 (India Standard Time)
Wed Sep 21 2022 00:00:00 GMT+0530 (India Standard Time)
Thu Sep 22 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Sep 23 2022 00:00:00 GMT+0530 (India Standard Time)
Sat Sep 24 2022 00:00:00 GMT+0530 (India Standard Time)
Sun Sep 25 2022 00:00:00 GMT+0530 (India Standard Time)
Mon Sep 26 2022 00:00:00 GMT+0530 (India Standard Time)
Tue Sep 27 2022 00:00:00 GMT+0530 (India Standard Time)
Wed Sep 28 2022 00:00:00 GMT+0530 (India Standard Time)
Thu Sep 29 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Sep 30 2022 00:00:00 GMT+0530 (India Standard Time)
For the month of Feb 2004:
Sun Feb 01 2004 00:00:00 GMT+0530 (India Standard Time)
Mon Feb 02 2004 00:00:00 GMT+0530 (India Standard Time)
Tue Feb 03 2004 00:00:00 GMT+0530 (India Standard Time)
Wed Feb 04 2004 00:00:00 GMT+0530 (India Standard Time)
Thu Feb 05 2004 00:00:00 GMT+0530 (India Standard Time)
Fri Feb 06 2004 00:00:00 GMT+0530 (India Standard Time)
Sat Feb 07 2004 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 08 2004 00:00:00 GMT+0530 (India Standard Time)
Mon Feb 09 2004 00:00:00 GMT+0530 (India Standard Time)
Tue Feb 10 2004 00:00:00 GMT+0530 (India Standard Time)
Wed Feb 11 2004 00:00:00 GMT+0530 (India Standard Time)
Thu Feb 12 2004 00:00:00 GMT+0530 (India Standard Time)
Fri Feb 13 2004 00:00:00 GMT+0530 (India Standard Time)
Sat Feb 14 2004 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 15 2004 00:00:00 GMT+0530 (India Standard Time)
Mon Feb 16 2004 00:00:00 GMT+0530 (India Standard Time)
Tue Feb 17 2004 00:00:00 GMT+0530 (India Standard Time)
Wed Feb 18 2004 00:00:00 GMT+0530 (India Standard Time)
Thu Feb 19 2004 00:00:00 GMT+0530 (India Standard Time)
Fri Feb 20 2004 00:00:00 GMT+0530 (India Standard Time)
Sat Feb 21 2004 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 22 2004 00:00:00 GMT+0530 (India Standard Time)
Mon Feb 23 2004 00:00:00 GMT+0530 (India Standard Time)
Tue Feb 24 2004 00:00:00 GMT+0530 (India Standard Time)
Wed Feb 25 2004 00:00:00 GMT+0530 (India Standard Time)
Thu Feb 26 2004 00:00:00 GMT+0530 (India Standard Time)
Fri Feb 27 2004 00:00:00 GMT+0530 (India Standard Time)
Sat Feb 28 2004 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 29 2004 00:00:00 GMT+0530 (India Standard Time)
Conclusion
In this article, we have learned how to get all the dates for both the current months as well as any given month. The user can also make similar user-defined functions with slight variations to achieve similar goals.

Rushabh Rupani is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Rushabh has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.