perumal N
perumal N

Reputation: 651

How to find dates between selected dates in angular 6?

I need dates for between selected 2 dates
- I Have first date as 17-12-2018 and last date as 22-12-2018
- Need dates between in this two dates in angular 6.
- I have searched but many results to find number of days for the selected 2 dates.

Upvotes: 0

Views: 1567

Answers (2)

abbas
abbas

Reputation: 66

use momentjs and make sure to choose a valid date format

let a = moment(new Date('12-17-2018'));
let b = moment(new Date('12-22-2018'));
let tempDate = a;
let daysCount = b.diff(a, 'days');

console.log('daysCount',daysCount);

let dates:Array<Date> = [];

for(let i= 1;i<daysCount ;i++){
  tempDate =tempDate.add(1, 'day');
  dates.push(tempDate.toDate());
}

console.log(dates);

Upvotes: 2

Alexandra
Alexandra

Reputation: 413

Are you looking for a list of date objects corresponding to the dates in between those 2? like {18-12-2018, 19-12-2018, 20-12-2018, 21-12-2018} ?

If so, you should write a function which takes two date objects and returns a list containing the dates in between them by (in a loop) creating a new date with the "day" value incremented, checking if that new date is earlier than the end date, if so, add it to your list. If not, exit the loop.

Upvotes: 0

Related Questions