0

In my application I am in need to show missing date range. I tried to get the first and last date but got struck to proceed further. when I tried, I got only the first missing date but the expected output is to display all missing dates from the start date to end date.

https://jsfiddle.net/bgef59x2/3/

https://stackblitz.com/edit/ionic-rknmsc?file=pages%2Fhome%2Fhome.ts

var data={
"dataObj"=[
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
]}

my startDate would be "2020-04-13" and enddate would be "2020-04-21" Expected output:

const result =["2020-04-14","2020-04-15","2020-04-16","2020-04-17","2020-04-18"]

Guide me to achieve the expected result in JavaScript/ Typescript. I have not included momentJs in my application.

web developer
  • 457
  • 2
  • 10
  • 25
  • What do you mean by missing? Are they undefined, null, empty strings? I reviewed the link you added, in which file is the problem? Can you update your original answer with more detail about the exact problem you have, what you've tried and why that hasn't worked so far? – Isolated May 11 '20 at 17:35
  • updated........ – web developer May 11 '20 at 17:47
  • Create an array and fill it with all dates between 'start' and 'end' date. Then, remove dates in your `dataObj` by `splic()` – Jack Ting May 11 '20 at 17:51
  • Are all your dates in order? Or do they need to be sorted first? Are you looking to fill only the missing dates? – Isolated May 11 '20 at 17:52
  • All the dates are in order. I want only the missing dates. – web developer May 11 '20 at 17:55
  • Does this answer your question? [Find missing day from array of dates javascript](https://stackoverflow.com/questions/40654656/find-missing-day-from-array-of-dates-javascript) – Heretic Monkey May 11 '20 at 18:21

2 Answers2

1

Web, just only create a llop from date to date, check if is in the array a push in an array

missingDates:string[]=[]
const missingDates: string[] = [];

const from = new Date(this.data[0].custom_date);
const to = new Date(this.data[this.data.length - 1].custom_date);
for (let fecha = from;fecha < to;fecha = new Date(fecha.getTime() + 24 * 60 * 60 * 1000)) {
  const date =fecha.getFullYear() +"-" +
    ("00" + (fecha.getMonth() + 1)).slice(-2) +"-" +
    ("00" + fecha.getDate()).slice(-2);

  if (!this.data.find(x => x.custom_date == date)) missingDates.push(date);
}
console.log(missingDates)
Eliseo
  • 50,109
  • 4
  • 29
  • 67
1

Better solution exists, but this script excludes dates as expected :

var dataObj = [
{"custom_date":"2020-04-13"},
{"custom_date":"2020-04-19"},
{"custom_date":"2020-04-20"},
{"custom_date":"2020-04-21"}
];
var dates = dataObj.map(d => d.custom_date)

console.log(getMissingDates(dates));

function getMissingDates(dates) {
  
  var datesParsed= dates.map(d => new Date(d)).sort()
  var allExpectedDates = [];
  var missingDates = [];
  var from = datesParsed[0];
  var to = datesParsed[datesParsed.length - 1];
  var current = from;
  while (current <= to) {
    allExpectedDates.push(formatDate(current));
    current.setDate(current.getDate() + 1);
  }

  return allExpectedDates.filter(el => {
    return dates.indexOf(el) === -1;
  });
}

// from https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd :
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-');
}
wawan
  • 497
  • 6
  • 11