Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get dates in between two dates with JavaScript.
// Returns an array of dates between the two dates
var getDates = function(startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
// Usage
var dates = getDates(new Date(2013,10,22), new Date(2013,11,25));
dates.forEach(function(date) {
console.log(date);
});
@andreibecker

This comment has been minimized.

Copy link

commented Jul 13, 2016

Very good, solve my problem.

@JockPerkins

This comment has been minimized.

Copy link

commented Jan 19, 2017

Worked perfectly, thanks!

@sibelius

This comment has been minimized.

Copy link

commented Mar 7, 2017

I made something more generic

it uses date-fns and does not mutate the array

import { addDays, addMonths, differenceInDays, differenceInMonths  } from 'date-fns';

import { PERIOD } from './PeriodEnumType';

export function dateRange(startDate, endDate, interval) {
  if (interval === PERIOD.DAY) {
    const days = differenceInDays(endDate, startDate);

    return [...Array(days+1).keys()].map((i) => addDays(startDate, i));
  }

  if (interval === PERIOD.MONTH) {
    const months = differenceInMonths(endDate, startDate);

    return [...Array(months+1).keys()].map((i) => addMonths(startDate, i));
  }
}
@barmao

This comment has been minimized.

Copy link

commented Sep 5, 2017

Works pretty well.. Thank you alot!!

@Ben52

This comment has been minimized.

Copy link

commented Sep 7, 2017

Here's a function to get a range of dates, split up by any key, ex. month, day, year, etc.

const moment = require('moment');

function getRangeOfDates(start, end, key, arr = [start.startOf(key)]) {
  
  if(start.isAfter(end)) throw new Error('start must precede end')
  
  const next = moment(start).add(1, key).startOf(key);
  
  
  if(next.isAfter(end, key)) return arr;
  

  return getRangeOfDates(next, end, key, arr.concat(next));
  
}

So if I wanted to get an array of months from the start of the year until a year from now, I would go:

const begin = moment().startOf('year')
const end = moment().add(1, 'year')

getRangeOfDates(begin, end, 'month');

Which would return:

[ moment("2017-01-01T00:00:00.000"),
  moment("2017-02-01T00:00:00.000"),
  moment("2017-03-01T00:00:00.000"),
  moment("2017-04-01T00:00:00.000"),
  moment("2017-05-01T00:00:00.000"),
  moment("2017-06-01T00:00:00.000"),
  moment("2017-07-01T00:00:00.000"),
  moment("2017-08-01T00:00:00.000"),
  moment("2017-09-01T00:00:00.000"),
  moment("2017-10-01T00:00:00.000"),
  moment("2017-11-01T00:00:00.000"),
  moment("2017-12-01T00:00:00.000"),
  moment("2018-01-01T00:00:00.000"),
  moment("2018-02-01T00:00:00.000"),
  moment("2018-03-01T00:00:00.000"),
  moment("2018-04-01T00:00:00.000"),
  moment("2018-05-01T00:00:00.000"),
  moment("2018-06-01T00:00:00.000"),
  moment("2018-07-01T00:00:00.000"),
  moment("2018-08-01T00:00:00.000"),
  moment("2018-09-01T00:00:00.000") ]

@baladkb

This comment has been minimized.

Copy link

commented Jan 5, 2018

@miguelmota thanks :octocat:

@googlicius

This comment has been minimized.

Copy link

commented Jan 10, 2018

Many thanks 😍

@jacintoface

This comment has been minimized.

Copy link

commented Jan 18, 2018

Thank You very much

@hriziya

This comment has been minimized.

Copy link

commented Apr 25, 2018

Thanks @Ben52

@cyberundertakor

This comment has been minimized.

Copy link

commented May 14, 2018

Thanks @Ben52

@aalexgabi

This comment has been minimized.

Copy link

commented May 18, 2018

My version which strips hours, minutes, seconds etc. and does not declare an inline function:

// Returns an array of dates between the two dates
const getDatesBetween = (startDate, endDate) => {
    const dates = [];

    // Strip hours minutes seconds etc.
    let currentDate = new Date(
        startDate.getFullYear(),
        startDate.getMonth(),
        startDate.getDate()
    );

    while (currentDate <= endDate) {
        dates.push(currentDate);

        currentDate = new Date(
            currentDate.getFullYear(),
            currentDate.getMonth(),
            currentDate.getDate() + 1, // Will increase month if over range
        );
    }

    return dates;
};

// Usage
const dates = getDates(new Date(2018, 0, 30, 11, 30), new Date(2018, 2, 2, 23, 59, 59)); 

console.log(dates);
@rocblue0112

This comment has been minimized.

Copy link

commented Jun 4, 2018

Thanks @aalexgabi, it resolved my problem 💯

@dibyasipu

This comment has been minimized.

Copy link

commented Jul 17, 2018

Help me sir,
how to check between two date range???
Example

from date=03-07-2018
to date=13-07-2018

how to check date range between them in javascript

@jameygleason

This comment has been minimized.

Copy link

commented Jul 25, 2018

@sibelius can you elaborate on how you use that function? For instance, it looks like you have another file you are including. What does that look like? Thanks in advance!

@wadadaaa

This comment has been minimized.

Copy link

commented Oct 24, 2018

import moment from 'moment';
getStandAge = startDate => { const age = moment().diff(startDate, 'months'); return isNaN(age) ? null : age; };

@pumpin101

This comment has been minimized.

Copy link

commented Oct 29, 2018

Please i would need help, i would like to do this using two date time picker and insert the dates in between the date time picker in a database but i dont know how to go about this in vb.net. Someone should help me please

@pumpin101

This comment has been minimized.

Copy link

commented Oct 29, 2018

I made something more generic

it uses date-fns and does not mutate the array

import { addDays, addMonths, differenceInDays, differenceInMonths  } from 'date-fns';

import { PERIOD } from './PeriodEnumType';

export function dateRange(startDate, endDate, interval) {
  if (interval === PERIOD.DAY) {
    const days = differenceInDays(endDate, startDate);

    return [...Array(days+1).keys()].map((i) => addDays(startDate, i));
  }

  if (interval === PERIOD.MONTH) {
    const months = differenceInMonths(endDate, startDate);

    return [...Array(months+1).keys()].map((i) => addMonths(startDate, i));
  }
}

Can u help using two date time picker and insert the dates in a database in vb.net

@zeekhan90

This comment has been minimized.

Copy link

commented Nov 8, 2018

Worked Perfectly.... thanks :-)

@nlt2390

This comment has been minimized.

Copy link

commented Nov 13, 2018

Here's a function to get a range of dates, split up by any key, ex. month, day, year, etc.

const moment = require('moment');

function getRangeOfDates(start, end, key, arr = [start.startOf(key)]) {
  
  if(start.isAfter(end)) throw new Error('start must precede end')
  
  const next = moment(start).add(1, key).startOf(key);
  
  
  if(next.isAfter(end, key)) return arr;
  

  return getRangeOfDates(next, end, key, arr.concat(next));
  
}

So if I wanted to get an array of months from the start of the year until a year from now, I would go:

const begin = moment().startOf('year')
const end = moment().add(1, 'year')

getRangeOfDates(begin, end, 'month');

Which would return:

[ moment("2017-01-01T00:00:00.000"),
  moment("2017-02-01T00:00:00.000"),
  moment("2017-03-01T00:00:00.000"),
  moment("2017-04-01T00:00:00.000"),
  moment("2017-05-01T00:00:00.000"),
  moment("2017-06-01T00:00:00.000"),
  moment("2017-07-01T00:00:00.000"),
  moment("2017-08-01T00:00:00.000"),
  moment("2017-09-01T00:00:00.000"),
  moment("2017-10-01T00:00:00.000"),
  moment("2017-11-01T00:00:00.000"),
  moment("2017-12-01T00:00:00.000"),
  moment("2018-01-01T00:00:00.000"),
  moment("2018-02-01T00:00:00.000"),
  moment("2018-03-01T00:00:00.000"),
  moment("2018-04-01T00:00:00.000"),
  moment("2018-05-01T00:00:00.000"),
  moment("2018-06-01T00:00:00.000"),
  moment("2018-07-01T00:00:00.000"),
  moment("2018-08-01T00:00:00.000"),
  moment("2018-09-01T00:00:00.000") ]

Thanks a lot

@nivduran

This comment has been minimized.

Copy link

commented Apr 16, 2019

It works but I have a problem with the month.
I run it like that:

var dates = getDates(new Date(2019,04,16), new Date(2019,04,17));                                                                                                           
dates.forEach(function(date) {
  console.log(date);
});

And the result is:

Thu May 16 2019 00:00:00 GMT+0300 (Israel Daylight Time)
Fri May 17 2019 00:00:00 GMT+0300 (Israel Daylight Time)

I received May instead of April, why? What am I missing?

@sgerin

This comment has been minimized.

Copy link

commented Apr 17, 2019

Hi @nivduran. When using the Date constructor like that:
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
keep in mind that the monthIndex parameter is 0-based. This means that January = 0 and December = 11.
In your case, you used 4 as the monthIndex parameter, which means the fifth month of the year which is May.

@shippudenmadara

This comment has been minimized.

Copy link

commented May 20, 2019

Thanks @aalexgabi

@kedomonzter

This comment has been minimized.

Copy link

commented Jul 5, 2019

How about filling missing dates inside arrays

var array=[
{date:"2016-11-17T00:00:00"}, //start
{date:"2016-11-19T00:00:00"},
{date:"2016-11-18T00:00:00"},
{date:"2016-11-21T00:00:00"},
{date:"2016-11-22T00:00:00"},
{date:"2016-11-23T00:00:00"},
{date:"2017-11-27T00:00:00"},//end
];

Actually very useful for charts that has missing data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.