Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to store all dates in an array present in between given two dates in JavaScript ?
Next article icon

How to store all dates in an array present in between given two dates in JavaScript ?

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
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 Content

  • Using push() Method
  • Using for loop and push() Method
  • Using concat method

Approach 1: Using push() Method

  • Select 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() Method

  • Get 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 ms
  • Push 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 method

The 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,   ...

Next Article
How to store all dates in an array present in between given two dates in JavaScript ?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-array
  • javascript-date
  • JavaScript-DSA
  • JavaScript-Questions

Similar Reads

    Return all dates between two dates in an array in PHP
    Returning all dates between two dates in an array means generating a list of all consecutive dates from the start date to the end date, inclusive, and storing each date as an element in an array for easy access.Here we have some common methods:Table of ContentUsing DatePeriod ClassUsing strtotime()
    4 min read
    How to calculate minutes between two dates in JavaScript ?
    Given two dates and the task is to get the number of minutes between them using JavaScript. Approach: Initialize both Date object.Subtract the older date from the new date. It will give the number of milliseconds from 1 January 1970.Convert milliseconds to minutes. Example 1: This example uses the c
    2 min read
    How to Calculate the Number of Days between Two Dates in JavaScript?
    Calculating the number of days between two dates is a common task in web development, especially when working with schedules, deadlines, or event planning. JavaScript provides a simple and effective way to perform this calculation by using different approaches. Whether you're comparing past and futu
    4 min read
    How to get tomorrow's date in a string format in JavaScript ?
    In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will ge
    2 min read
    How to Sort a Multidimensional Array in JavaScript by Date ?
    Sorting a Multidimensional Array by date consists of ordering the inner arrays based on the date values. This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order. Below are
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences