0

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.

perumal N
  • 641
  • 2
  • 10
  • 27
  • Like, the number of days between the two dates? A simple search on SO or Google would help you find this. – Jeto Dec 27 '18 at 14:19
  • I tried but everyone saying to find number of days only – perumal N Dec 27 '18 at 14:20
  • Possible duplicate of [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – Jeto Dec 27 '18 at 14:25
  • actually before i did in javascript and php also... but i does not in angular – perumal N Dec 27 '18 at 14:27
  • Angular uses typescript, and typescript compiles to JS. You can transform any JS code into typescript and vice versa. – Jeto Dec 27 '18 at 14:28

2 Answers2

2

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);
abbas
  • 66
  • 1
  • 5
0

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.

Alexandra
  • 423
  • 5
  • 10