-4

I am getting an array of day dates from an API

The data is sorted and more than one date is skipped.

I have to create the daily, weekly, and monthly reports from the below data. If there are any missing dates, then I have to add the missing dates into the array with the previous date's 'Workcompletion' value.

I found a similar question on here: Find missing day from array of dates javascript, but those answers don't handle the more than one day missing.

1: {Date: '2021-09-24T00:00:00.000Z', WorkCompletion: 0}
2: {Date: '2021-09-25T00:00:00.000Z', WorkCompletion: 0}
3: {Date: '2021-09-26T00:00:00.000Z', WorkCompletion: 0}
4: {Date: '2021-09-27T00:00:00.000Z', WorkCompletion: 0}
5: {Date: '2021-09-28T00:00:00.000Z', WorkCompletion: 0}
6: {Date: '2021-09-29T00:00:00.000Z', WorkCompletion: 0}
7: {Date: '2021-09-30T00:00:00.000Z', WorkCompletion: 0}
8: {Date: '2021-10-01T00:00:00.000Z', WorkCompletion: 0}
9: {Date: '2021-10-02T00:00:00.000Z', WorkCompletion: 0}
10: {Date: '2021-10-03T00:00:00.000Z', WorkCompletion: 0}
11: {Date: '2021-10-04T00:00:00.000Z', WorkCompletion: 0}
12: {Date: '2021-10-05T00:00:00.000Z', WorkCompletion: 0}
13: {Date: '2021-10-06T00:00:00.000Z', WorkCompletion: 0}
14: {Date: '2021-10-07T00:00:00.000Z', WorkCompletion: 0}
15: {Date: '2021-10-08T00:00:00.000Z', WorkCompletion: 0}
16: {Date: '2021-10-10T00:00:00.000Z', WorkCompletion: 0}
17: {Date: '2021-10-11T00:00:00.000Z', WorkCompletion: 0}
18: {Date: '2021-10-12T00:00:00.000Z', WorkCompletion: 0}
19: {Date: '2021-10-13T00:00:00.000Z', WorkCompletion: 0}
20: {Date: '2021-10-14T00:00:00.000Z', WorkCompletion: 1.35}
21: {Date: '2021-10-15T00:00:00.000Z', WorkCompletion: 1.35}
22: {Date: '2021-10-16T00:00:00.000Z', WorkCompletion: 1.35}
23: {Date: '2021-10-17T00:00:00.000Z', WorkCompletion: 1.35}
24: {Date: '2021-10-18T00:00:00.000Z', WorkCompletion: 1.35}
25: {Date: '2021-10-19T00:00:00.000Z', WorkCompletion: 1.35}
26: {Date: '2021-10-20T00:00:00.000Z', WorkCompletion: 1.35}
27: {Date: '2021-10-21T00:00:00.000Z', WorkCompletion: 1.35}
28: {Date: '2021-10-22T00:00:00.000Z', WorkCompletion: 1.35}
29: {Date: '2021-10-23T00:00:00.000Z', WorkCompletion: 1.35}
30: {Date: '2021-10-24T00:00:00.000Z', WorkCompletion: 1.35}
31: {Date: '2021-10-25T00:00:00.000Z', WorkCompletion: 1.35}
32: {Date: '2021-10-26T00:00:00.000Z', WorkCompletion: 1.35}
33: {Date: '2021-10-27T00:00:00.000Z', WorkCompletion: 1.35}
34: {Date: '2021-10-28T00:00:00.000Z', WorkCompletion: 1.35}
35: {Date: '2021-10-29T00:00:00.000Z', WorkCompletion: 1.35}
36: {Date: '2021-10-30T00:00:00.000Z', WorkCompletion: 1.35}
37: {Date: '2021-10-31T00:00:00.000Z', WorkCompletion: 1.35}
38: {Date: '2021-11-01T00:00:00.000Z', WorkCompletion: 1.35}
39: {Date: '2021-11-02T00:00:00.000Z', WorkCompletion: 1.35}
40: {Date: '2021-11-03T00:00:00.000Z', WorkCompletion: 1.35}
41: {Date: '2021-11-04T00:00:00.000Z', WorkCompletion: 1.35}
42: {Date: '2021-11-05T00:00:00.000Z', WorkCompletion: 1.35}
43: {Date: '2021-11-07T00:00:00.000Z', WorkCompletion: 1.35}
44: {Date: '2021-11-08T00:00:00.000Z', WorkCompletion: 1.35}
45: {Date: '2021-11-09T00:00:00.000Z', WorkCompletion: 1.35}
46: {Date: '2021-11-10T00:00:00.000Z', WorkCompletion: 1.35}
47: {Date: '2021-11-11T00:00:00.000Z', WorkCompletion: 1.35}
48: {Date: '2021-11-12T00:00:00.000Z', WorkCompletion: 1.35}
49: {Date: '2021-11-13T00:00:00.000Z', WorkCompletion: 1.35}
50: {Date: '2021-11-14T00:00:00.000Z', WorkCompletion: 1.35}
51: {Date: '2021-11-15T00:00:00.000Z', WorkCompletion: 1.35}
52: {Date: '2021-11-16T00:00:00.000Z', WorkCompletion: 1.35}
53: {Date: '2021-11-18T00:00:00.000Z', WorkCompletion: 3.27}
54: {Date: '2021-11-21T00:00:00.000Z', WorkCompletion: 10.87}
55: {Date: '2021-11-22T00:00:00.000Z', WorkCompletion: 12.76}
56: {Date: '2021-11-28T00:00:00.000Z', WorkCompletion: 19.5}
57: {Date: '2021-11-29T00:00:00.000Z', WorkCompletion: 19.5}
58: {Date: '2021-11-30T00:00:00.000Z', WorkCompletion: 19.5}
59: {Date: '2021-12-01T00:00:00.000Z', WorkCompletion: 20.5}
60: {Date: '2021-12-02T00:00:00.000Z', WorkCompletion: 21.49}
61: {Date: '2021-12-05T00:00:00.000Z', WorkCompletion: 22.16}
62: {Date: '2021-12-06T00:00:00.000Z', WorkCompletion: 22.16}
63: {Date: '2021-12-07T00:00:00.000Z', WorkCompletion: 25.51}
64: {Date: '2021-12-08T00:00:00.000Z', WorkCompletion: 25.51}
65: {Date: '2021-12-09T00:00:00.000Z', WorkCompletion: 26.09}
66: {Date: '2021-12-10T00:00:00.000Z', WorkCompletion: 27.67}
67: {Date: '2021-12-11T00:00:00.000Z', WorkCompletion: 27.67}
68: {Date: '2021-12-12T00:00:00.000Z', WorkCompletion: 27.67}
69: {Date: '2021-12-13T00:00:00.000Z', WorkCompletion: 29.42}
70: {Date: '2021-12-14T00:00:00.000Z', WorkCompletion: 30.12}
71: {Date: '2021-12-15T00:00:00.000Z', WorkCompletion: 33.5}
72: {Date: '2021-12-16T00:00:00.000Z', WorkCompletion: 34.54}
73: {Date: '2021-12-17T00:00:00.000Z', WorkCompletion: 36}
74: {Date: '2021-12-18T00:00:00.000Z', WorkCompletion: 36}
75: {Date: '2021-12-19T00:00:00.000Z', WorkCompletion: 36}
76: {Date: '2021-12-21T00:00:00.000Z', WorkCompletion: 37.36}
77: {Date: '2021-12-22T00:00:00.000Z', WorkCompletion: 40.29}
78: {Date: '2021-12-23T00:00:00.000Z', WorkCompletion: 40.29}
79: {Date: '2021-12-24T00:00:00.000Z', WorkCompletion: 40.29}
80: {Date: '2021-12-25T00:00:00.000Z', WorkCompletion: 40.29}
81: {Date: '2021-12-26T00:00:00.000Z', WorkCompletion: 40.29}
82: {Date: '2021-12-27T00:00:00.000Z', WorkCompletion: 41.78}
83: {Date: '2021-12-28T00:00:00.000Z', WorkCompletion: 42.88}
84: {Date: '2021-12-29T00:00:00.000Z', WorkCompletion: 43.48}
85: {Date: '2021-12-30T00:00:00.000Z', WorkCompletion: 43.48}
86: {Date: '2021-12-31T00:00:00.000Z', WorkCompletion: 43.48}
87: {Date: '2022-01-01T00:00:00.000Z', WorkCompletion: 43.48}
88: {Date: '2022-01-02T00:00:00.000Z', WorkCompletion: 43.48}
89: {Date: '2022-01-03T00:00:00.000Z', WorkCompletion: 43.48}
90: {Date: '2022-01-04T00:00:00.000Z', WorkCompletion: 48.02}
91: {Date: '2022-01-05T00:00:00.000Z', WorkCompletion: 49.43}
92: {Date: '2022-01-06T00:00:00.000Z', WorkCompletion: 50.84}
93: {Date: '2022-01-07T00:00:00.000Z', WorkCompletion: 52.25}
94: {Date: '2022-01-08T00:00:00.000Z', WorkCompletion: 52.25}
95: {Date: '2022-01-09T00:00:00.000Z', WorkCompletion: 52.25}


  [1]: https://stackoverflow.com/questions/40654656/find-missing-day-from-array-of-dates-javascript
3
  • 1
    The selected answer at that link does appear to handle multiple days missing (between the earliest and latest dates indicated). Commented Jan 10, 2022 at 16:18
  • 1
    Does this answer your question? Find missing day from array of dates javascript Commented Jan 10, 2022 at 16:36
  • @jarmod I tested with all the solutions but didn't work. However, I found a solution in here stackoverflow.com/questions/43651385/… Commented Jan 10, 2022 at 16:46

1 Answer 1

0
const testing = (data) => {
    const firstDate = data[0].Date;
    const lastDate = data[data.length - 1].Date;
    const dates = [...Array(Date.parse(lastDate) / 86400000 - Date.parse(firstDate) / 86400000 + 1).keys()].map((k) => {
        return (
            new Date(86400000 * k + Date.parse(firstDate))
                .toISOString()
                .slice(0, 10)
                .concat('T00:00:00.000Z')
        );
    });
    let res = [];
    for (let i = 0, j = 0; i < dates.length; i++) {
        res[i] = {
            Date: dates[i],
            WorkCompletion: dates[i] === data[j].Date ? data[j++].WorkCompletion : data[i - 1].WorkCompletion,
        };
    }
    console.table(res);
};
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.