Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active February 7, 2024 23:43
Show Gist options
  • Save miguelmota/7905510 to your computer and use it in GitHub Desktop.
Save miguelmota/7905510 to your computer and use it in GitHub Desktop.
Get dates in between two dates with JavaScript.
// Returns an array of dates between the two dates
function getDates (startDate, endDate) {
const dates = []
let currentDate = startDate
const addDays = function (days) {
const 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
const dates = getDates(new Date(2013, 10, 22), new Date(2013, 11, 25))
dates.forEach(function (date) {
console.log(date)
})
@anoop0567
Copy link

Please provide code for getting hours in between two dates with JavaScript.

Example

let date1 = new Date(1582545600000) // 24th Feb 17:30
let date2 = new Date(1582574400000) // 25th Feb 01:30
getHourWiseDates(date1, date2);

Output
[ { start_hour: 1582545600000 // 24th Feb 17:30 end_hour: 1582547399099 // 24th Feb 17:59 }, { start_hour: 1582547400000 // 24th Feb 18:00 end_hour: 1582550999099 // 24th Feb 18:59 } ... ... ... { start_hour: 1582572600000 //25th Feb 01:00 end_hour: 1582574400000 // 25th Feb 01:30 } ]

@kuldeep-pixer
Copy link

Screenshot_1

i want this type of array like shortmonths name and years starting date is Sep 2017 to current months and year

@AntonOfTheWoods
Copy link

AntonOfTheWoods commented Nov 23, 2021

Using dayjs and accepting any of the dayjs supported types (from millisecond to year), in typescript and returning either Dates or numbers (unix timestamps in milliseconds)

export function dateRange(
  start: Date | number,
  end: Date | number,
  interval: "millisecond" | "second" | "minute" | "hour" | "day" | "week" | "month" | "year",
  asUnixTimestampsMS = false,
): (number | Date)[] {
  const startDate = dayjs(start);
  const endDate = dayjs(end);
  const diffInUnits = endDate.diff(startDate, interval)
  return Array.from(Array(diffInUnits + 1).keys()).map((i) => {
    return asUnixTimestampsMS ? startDate.add(i, interval).valueOf() : startDate.add(i, interval).toDate()
  });
}

@Blackie31
Copy link

I'm selected dates
24-01-2022
25-01-2022

but I get

Thu Feb 24 2022 00:00:00 GMT+0530 (India Standard Time)
Fri Feb 25 2022 00:00:00 GMT+0530 (India Standard Time)

and my code

var dests = getDates(new Date(des[2],des[1],des[0]), new Date(des1[2],des1[1],des1[0]));
dests.forEach(function(date) {
console.log(dests);
});
how to fix it?

@ekopras18
Copy link

Perfectly.. Thanks u bro

@ekopras18
Copy link

This for Get range dates, work for me

var start = '24-01-2022';
var end = '26-01-2022';
var dates = getDates(new Date(start), new Date(end));
// dates.forEach(function(date) {
// console.log(date);
// });

// Get Range Dates
console.log(dates.length);

@PaulSebalu
Copy link

Life saver. Thank you @miguelmota.

@anhtuank7c
Copy link

I would convert it to TypeScript version.

const getDates = (startDate: Date, endDate: Date) => {
  const dates = []
  let currentDate = startDate
  const addDays = (currentDate: Date, days: number) => {
    const date = new Date(currentDate)
    date.setDate(date.getDate() + days)
    return date
  }
  while (currentDate <= endDate) {
    dates.push(currentDate)
    currentDate = addDays(currentDate, 1)
  }
  return dates
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment