0

I have one date rang json and I need to generate missing dates and store into array but how can I do have no idea.

Here below json:

0:{ AvailabilityStatus : 1 
    DateFrom : "2018-08-04 00:00" 
    DateTo   : "2018-08-11 00:00" }
1:{ Status : 1 
    DateFrom : "2018-08-11 00:00" 
    DateTo   : "2018-08-18 00:00" }
 2:{ Status : 1 
     DateFrom : "2018-09-01 00:00" 
     DateTo   : "2018-09-08 00:00" }
 3:{ Status : 1 
     DateFrom : "2018-09-08 00:00" 
     DateTo   : "2018-09-15 00:00" }

So above from the json missing date range is 2018-08-19 to 2018-08-31. so I need to generate missing dates like this 2018-08-19,2018-08-20,...,2018-08-31 and store into an array.

1 Answer 1

1

With the momentJs library (https://momentjs.com/docs/), you could do something like :

var startRange = moment.utc("2018-08-18 00:00").add(1, 'days'); // add one day to the start to not include the start date;
var endRange = moment.utc("2018-09-01 00:00");
var result = [];

while ( startRange.isBefore(endRange, 'day') ) {
  result.push(startRange.toDate());
  startRange.add(1, 'days');
}

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.