-
Star
(108)
You must be signed in to star a gist -
Fork
(27)
You must be signed in to fork a gist
-
-
Save miguelmota/7905510 to your computer and use it in GitHub Desktop.
// 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) | |
}) |
Very good, thank you sir!
if you are looking for specific days of the week this will give them to you granted you provide a string array, otherwise, just dont use the if statement .
import * as moment from "moment";
import * as _ from 'lodash';
getDatesOfDays(startDate: moment.Moment, endDate: moment.Moment, dayArray: string[]) {
var range = [];
var dayNames = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
while (moment(startDate) <= moment(endDate)){
if (dayArray.includes(dayNames[startDate.day()])) {
range.push(startDate.clone());
}
startDate.add(1 , 'days');
}
return range;
}
https://gist.github.com/miguelmota/7905510#file-getdates-js
It's very pretty.
I solved my problem.
Node.js & ES6
get days diff between two dates and output all weekdays in between including start and end dates.
return an array of dates.
const moment = require("moment");
const getDatesDiff = (start_date, end_date, date_format = "YYYY-MM-DD") => {
const getDateAsArray = date => {
return moment(date.split(/\D+/), date_format);
};
const diff = getDateAsArray(end_date).diff(getDateAsArray(start_date), "days") + 1;
const dates = [];
for (let i = 0; i < diff; i++) {
const nextDate = getDateAsArray(start_date).add(i, "day");
const isWeekEndDay = nextDate.isoWeekday() > 5;
if (!isWeekEndDay)
dates.push(nextDate.format(date_format))
}
return dates;
};
Use:
const date_log = getDaysDiff ('2019-10-17', '2019-10-25');
console.log(date_log);
output:
date_log =
[ '2019-10-17',
'2019-10-18',
'2019-10-21',
'2019-10-22',
'2019-10-23',
'2019-10-24',
'2019-10-25'
]
I tried this and it was returned wrong
getDates(new Date(2019,12,22), new Date(2020,1,7));
Wed Jan 22 2020 00:00:00 GMT+0700 (Indochina Time)
Thu Jan 23 2020 00:00:00 GMT+0700 (Indochina Time)
Jan 24 2020 00:00:00 GMT+0700 (Indochina Time)
Sat Jan 25 2020 00:00:00 GMT+0700 (Indochina Time)
Sun Jan 26 2020 00:00:00 GMT+0700 (Indochina Time)
Mon Jan 27 2020 00:00:00 GMT+0700 (Indochina Time)
Tue Jan 28 2020 00:00:00 GMT+0700 (Indochina Time)
Wed Jan 29 2020 00:00:00 GMT+0700 (Indochina Time)
Thu Jan 30 2020 00:00:00 GMT+0700 (Indochina Time)
Fri Jan 31 2020 00:00:00 GMT+0700 (Indochina Time)
Sat Feb 01 2020 00:00:00 GMT+0700 (Indochina Time)
Sun Feb 02 2020 00:00:00 GMT+0700 (Indochina Time)
Mon Feb 03 2020 00:00:00 GMT+0700 (Indochina Time)
Tue Feb 04 2020 00:00:00 GMT+0700 (Indochina Time)
Wed Feb 05 2020 00:00:00 GMT+0700 (Indochina Time)
Thu Feb 06 2020 00:00:00 GMT+0700 (Indochina Time)
Fri Feb 07 2020 00:00:00 GMT+0700 (Indochina Time)
Thanks for the code.
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 } ]
Using dayjs
and accepting any of the dayjs
supported types (from millisecond to year), in typescript and returning either Date
s or number
s (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()
});
}
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?
Perfectly.. Thanks u bro
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);
Life saver. Thank you @miguelmota.
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
}
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 }
Thank you
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.