0

I have a following date fields

{ tripScheduleStartDate: '2018-12-05T18:30:00.000Z',
  tripScheduleEndDate: '2018-12-07T18:30:00.000Z',
}

How can i get datetime array from start to end, something like this

[ { date: '2018-12-05T18:30:00.000Z' }, { date: '2018-12-06T18:30:00.000Z' },{ date: '2018-12-07T18:30:00.000Z' } ]
Dino Maria
  • 11
  • 2
  • 5
    Possible duplicate of [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – Maor Refaeli Dec 05 '18 at 16:53

3 Answers3

1

PSEUDO-CODE

Time start = x;
Time end = y
tmpTime = x;
timeArray = [];
While (tmpTime < y) {
timeArray.Add(tmpTime)
tmpTime = tmpTime.AddDays(1);
}
Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
0

You could use eachDay from date-fns.

{ 
  tripScheduleStartDate: '2018-12-05T18:30:00.000Z',
  tripScheduleEndDate: '2018-12-07T18:30:00.000Z',
}

Import: import eachDay from 'date-fns/each_day'

Usage: eachDay(tripScheduleStartDate, tripScheduleEndDate)

Dan
  • 8,041
  • 8
  • 41
  • 72
0

This may help you

Date.prototype.addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
    }

    function gettheDates(sDate, eDate) {
        var dateArray = new Array();
        var ctDate = sDate;
        while (ctDate <= eDate) {
            dateArray.push(new Date (ctDate ));
            ctDate = ctDate .addDays(1);
        }
            return dateArray;
    }
Aji
  • 423
  • 8
  • 23