How to store all dates in an array present in between given two dates in JavaScript ? Last Updated : 03 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two dates the task is to get the array of dates between the two given dates using JavaScript. Below are the following approaches:Table of ContentUsing push() MethodUsing for loop and push() MethodUsing concat methodApproach 1: Using push() MethodSelect the first and last date and store it in a variable.Check if the start date is less than the stop date then push the current date in an array and increment its value by 1 day.Repeat the above step until currentDate equal to the last date.Example: In this example, the array of dates is determined by the above approach. JavaScript Date.prototype.addDay = function (days) { let date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } function getDate(strDate, stpDate) { let dArray = new Array(); let cDate = strDate; while (cDate <= stpDate) { // Adding the date to array dArray.push(new Date(cDate) + '<br>'); // Increment the date by 1 day cDate = cDate.addDay(1); } return dArray; } function GFG_Fun() { let startDate = new Date(); // Making lastDate equal to 4 more days // from startDate. let endDate = startDate.addDay(4); console.log(getDate(startDate, endDate)); } GFG_Fun(); Output[ 'Tue Jul 18 2023 18:59:06 GMT+0000 (Coordinated Universal Time)<br>', 'Wed Jul 19 2023 18:59:06 GMT+0000 (Coordinated Universal Time)<br>', 'Thu Jul 20 2023 18:59:06 GMT+0000 (Coordinated Univ... Approach 2: Using for loop and push() MethodGet the first and last date and store it into a variable.Calculate 1 day equivalent in milliseconds called _1Day.Set a variable equal to the start date, called msPush ms (milli-seconds) in form of a date in an array and increment its value by _1Day.Repeat the above step until ms is equal to the last date.Example: In this example, the array of dates is determined by the above approach. JavaScript Date.prototype.addDay = function (days) { let date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }; function getDates(date1, date2) { let _1Day = 24 * 3600 * 1000; // Date[] keeps all the dates let dates = []; for (let ms = date1.getTime(), last = date2.getTime(); ms <= last; ms += _1Day) { dates.push(new Date(ms)); } return dates; } function GFG_Fun() { let startDate = new Date(); // Making lastDate equal to 4 more days // from startDate let endDate = startDate.addDay(4); console.log(getDates(startDate, endDate)); } GFG_Fun(); Output[ 2023-07-18T19:12:03.831Z, 2023-07-19T19:12:03.831Z, 2023-07-20T19:12:03.831Z, 2023-07-21T19:12:03.831Z, 2023-07-22T19:12:03.831Z ]Approach 3: Using concat methodThe function getAllDates uses a for loop to iterate through dates between startDate and endDate, incrementing by one day each iteration. Dates are then concatenated into an array.Example: JavaScript function getAllDates(startDate, endDate) { let dates = []; for (let date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1)) { dates = dates.concat(new Date(date)); } return dates; } const startDate = new Date('2023-01-01'); const endDate = new Date('2023-01-10'); const result = getAllDates(startDate, endDate); console.log(result); Output[ 2023-01-01T00:00:00.000Z, 2023-01-02T00:00:00.000Z, 2023-01-03T00:00:00.000Z, 2023-01-04T00:00:00.000Z, 2023-01-05T00:00:00.000Z, 2023-01-06T00:00:00.000Z, 2023-01-07T00:00:00.000Z, ... Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array javascript-date JavaScript-DSA JavaScript-Questions +2 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like