// 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); | |
}); |
This comment has been minimized.
This comment has been minimized.
Worked perfectly, thanks! |
This comment has been minimized.
This comment has been minimized.
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));
}
} |
This comment has been minimized.
This comment has been minimized.
Works pretty well.. Thank you alot!! |
This comment has been minimized.
This comment has been minimized.
Here's a function to get a range of dates, split up by any key, ex. month, day, year, etc.
So if I wanted to get an array of months from the start of the year until a year from now, I would go:
Which would return:
|
This comment has been minimized.
This comment has been minimized.
@miguelmota thanks |
This comment has been minimized.
This comment has been minimized.
Many thanks |
This comment has been minimized.
This comment has been minimized.
Thank You very much |
This comment has been minimized.
This comment has been minimized.
Thanks @Ben52 |
This comment has been minimized.
This comment has been minimized.
Thanks @Ben52 |
This comment has been minimized.
This comment has been minimized.
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); |
This comment has been minimized.
This comment has been minimized.
Thanks @aalexgabi, it resolved my problem |
This comment has been minimized.
This comment has been minimized.
Help me sir,
|
This comment has been minimized.
This comment has been minimized.
@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! |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Can u help using two date time picker and insert the dates in a database in vb.net |
This comment has been minimized.
This comment has been minimized.
Worked Perfectly.... thanks :-) |
This comment has been minimized.
This comment has been minimized.
Thanks a lot |
This comment has been minimized.
This comment has been minimized.
It works but I have a problem with the month.
And the result is:
I received May instead of April, why? What am I missing? |
This comment has been minimized.
This comment has been minimized.
Hi @nivduran. When using the Date constructor like that: |
This comment has been minimized.
This comment has been minimized.
Thanks @aalexgabi |
This comment has been minimized.
This comment has been minimized.
How about filling missing dates inside arrays var array=[ Actually very useful for charts that has missing data. |
This comment has been minimized.
This comment has been minimized.
Very good, thank you sir! |
This comment has been minimized.
This comment has been minimized.
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 .
|
This comment has been minimized.
This comment has been minimized.
https://gist.github.com/miguelmota/7905510#file-getdates-js |
This comment has been minimized.
This comment has been minimized.
Node.js & ES6 get days diff between two dates and output all weekdays in between including start and end 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:
|
This comment has been minimized.
This comment has been minimized.
I tried this and it was returned wrong
|
This comment has been minimized.
This comment has been minimized.
Thanks for the code. |
This comment has been minimized.
This comment has been minimized.
Please provide code for getting hours in between two dates with JavaScript. Example
Output |
This comment has been minimized.
Very good, solve my problem.