Home > front end >  arrange array of dates in weeks
arrange array of dates in weeks

Time:01-28

I have an array containing dates of startTime and their total spend time, I want to arrange the array into weeks output should be

//output = [{weekOf : "1/14/2022 - 1/21/2022"  ,  totalTime : "0:0:12"}]

and array of sample data of dates is

const dates = [ 
    { startTime: "Fri Jan 14 2022 10:36:34 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:12" }, 
    { startTime: "Fri Jan 14 2022 10:39:08 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:6" }, 
    { startTime: "Mon Jan 17 2022 13:32:42 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:17" }, 
    { startTime: "Mon Jan 17 2022 13:34:57 GMT-0500 (Eastern Standard Time)",  totalTime: "0:37:35" }, 
    { startTime: "Mon Jan 17 2022 13:58:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:14:2" }, 
    { startTime: "Mon Jan 17 2022 14:48:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:2:27" }, 
    { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
    { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:8:17" }, 
    { startTime: "Tue Jan 18 2022 11:56:06 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:27" }, 
    { startTime: "Tue Jan 18 2022 11:56:34 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:40" }, 
    { startTime: "Tue Jan 18 2022 12:06:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:2" }, 
    { startTime: "Tue Jan 18 2022 12:07:18 GMT-0500 (Eastern Standard Time)", totalTime: "0:13:11" }, 
    { startTime: "Tue Jan 18 2022 12:20:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:52" }, 
    { startTime: "Tue Jan 18 2022 12:21:23 GMT-0500 (Eastern Standard Time)", totalTime: "0:55:41" },
    { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)", totalTime: "0:0:0" }, 
    { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:26" }, 
    { startTime: "Wed Jan 19 2022 14:15:26 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
    { startTime: "Tue Jan 25 2022 11:22:06 GMT-0500 (Eastern Standard Time)", totalTime: "0:4:11" }, 
    { startTime: "Tue Jan 25 2022 11:19:46 GMT-0500 (Eastern Standard Time)", totalTime: "0:6:35" }, 
    { startTime: "Tue Jan 25 2022 12:50:47 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:51" },
    { startTime: "Tue Jan 25 2022 13:00:39 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:25" }
 ]

CodePudding user response:

You should use the momentjs

import moment from "moment";

const dates = [ 
    { startTime: "Fri Jan 14 2022 10:36:34 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:12" }, 
    { startTime: "Fri Jan 14 2022 10:39:08 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:6" }, 
    { startTime: "Mon Jan 17 2022 13:32:42 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:17" }, 
    { startTime: "Mon Jan 17 2022 13:34:57 GMT-0500 (Eastern Standard Time)",  totalTime: "0:37:35" }, 
    { startTime: "Mon Jan 17 2022 13:58:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:14:2" }, 
    { startTime: "Mon Jan 17 2022 14:48:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:2:27" }, 
    { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
    { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:8:17" }, 
    { startTime: "Tue Jan 18 2022 11:56:06 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:27" }, 
    { startTime: "Tue Jan 18 2022 11:56:34 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:40" }, 
    { startTime: "Tue Jan 18 2022 12:06:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:2" }, 
    { startTime: "Tue Jan 18 2022 12:07:18 GMT-0500 (Eastern Standard Time)", totalTime: "0:13:11" }, 
    { startTime: "Tue Jan 18 2022 12:20:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:52" }, 
    { startTime: "Tue Jan 18 2022 12:21:23 GMT-0500 (Eastern Standard Time)", totalTime: "0:55:41" },
    { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)", totalTime: "0:0:0" }, 
    { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:26" }, 
    { startTime: "Wed Jan 19 2022 14:15:26 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
    { startTime: "Tue Jan 25 2022 11:22:06 GMT-0500 (Eastern Standard Time)", totalTime: "0:4:11" }, 
    { startTime: "Tue Jan 25 2022 11:19:46 GMT-0500 (Eastern Standard Time)", totalTime: "0:6:35" }, 
    { startTime: "Tue Jan 25 2022 12:50:47 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:51" },
    { startTime: "Tue Jan 25 2022 13:00:39 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:25" }
 ]


const output = {};
const convertToNumber = (number) => {
    try {
        const clearedNumber = String(number).replace(/0 /, ""); // Clear leading 0
        return Number(clearedNumber);
    } catch (error) { // In case of number converting error return 0
        return 0;
    }
};

dates.forEach(({startTime, totalTime}) => {
  const startDate = moment(startTime).startOf("week").format("DD/MM/YYYY");  
  const endDate = moment(startTime).endOf("week").format("DD/MM/YYYY");

  const [hours, mins, secs] = totalTime.split(":").map(number => convertToNumber(number));
  const totalTimeInSeconds =  hours*60*60   mins*60   secs;

  output[`${startDate} - ${endDate}`] = (output[`${startDate} - ${endDate}`] || 0)   totalTimeInSeconds;
});

const preparedOutput = [];
for (const key in output) {
    preparedOutput.push({weekOf: key, totalTime: moment(output[key]*1000).utc().format("HH:mm:ss")})
}

Finally preparedOutput should be wanted format

CodePudding user response:

I would pre-load a list with the weeks your data covers. Look for the minimum and maximum dates in your source data; find the first Monday date that is before your minimum; then loop through weeks until your maximum is included in the list of weeks. Initialize a "totalTime" value to 0:00:00 for each week of course.

After that, you'll need an overlap test function. Given a start and end date (or just start date and assume the end date is 7 days later) and the date you are testing, does the date fall into that date range?

Now, you can loop through your source data and do a nested loop through each of the weeks. Check to see if the source date falls within the week using your overlap method, and if so: add the time to the totalTime, and then break from the inner loop (to stop checking more weeks).

When you're done with the looping to add up total time, you can output the list you made with weeks and total time.

CodePudding user response:

const dates = [ 
        { startTime: "Fri Jan 14 2022 10:36:34 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:12" }, 
        { startTime: "Fri Jan 14 2022 10:39:08 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:6" }, 
        { startTime: "Mon Jan 17 2022 13:32:42 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:17" }, 
        { startTime: "Mon Jan 17 2022 13:34:57 GMT-0500 (Eastern Standard Time)",  totalTime: "0:37:35" }, 
        { startTime: "Mon Jan 17 2022 13:58:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:14:2" }, 
        { startTime: "Mon Jan 17 2022 14:48:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:2:27" }, 
        { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
        { startTime: "Tue Jan 18 2022 11:47:47 GMT-0500 (Eastern Standard Time)",  totalTime: "0:8:17" }, 
        { startTime: "Tue Jan 18 2022 11:56:06 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:27" }, 
        { startTime: "Tue Jan 18 2022 11:56:34 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:40" }, 
        { startTime: "Tue Jan 18 2022 12:06:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:2" }, 
        { startTime: "Tue Jan 18 2022 12:07:18 GMT-0500 (Eastern Standard Time)", totalTime: "0:13:11" }, 
        { startTime: "Tue Jan 18 2022 12:20:30 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:52" }, 
        { startTime: "Tue Jan 18 2022 12:21:23 GMT-0500 (Eastern Standard Time)", totalTime: "0:55:41" },
        { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)", totalTime: "0:0:0" }, 
        { startTime: "Wed Jan 19 2022 13:19:15 GMT-0500 (Eastern Standard Time)",  totalTime: "0:1:26" }, 
        { startTime: "Wed Jan 19 2022 14:15:26 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:0" }, 
        { startTime: "Tue Jan 25 2022 11:22:06 GMT-0500 (Eastern Standard Time)", totalTime: "0:4:11" }, 
        { startTime: "Tue Jan 25 2022 11:19:46 GMT-0500 (Eastern Standard Time)", totalTime: "0:6:35" }, 
        { startTime: "Tue Jan 25 2022 12:50:47 GMT-0500 (Eastern Standard Time)", totalTime: "0:9:51" },
        { startTime: "Tue Jan 25 2022 13:00:39 GMT-0500 (Eastern Standard Time)",  totalTime: "0:0:25" }
     ]
     
const output = {};
const convertToNumber = (number) => {
    try {
        const clearedNumber = String(number).replace(/0 /, ""); // Clear leading 0
        return Number(clearedNumber);
    } catch (error) { // In case of number converting error return 0
        return 0;
    }
};

dates.forEach(({startTime, totalTime}) => {
  const startWeek = new Date(startTime).getDate()   `/${new Date(startTime).getMonth()}/${new Date(startTime).getFullYear()}`;
  const endWeek = new Date(startTime).getDate()   7   `/${new Date(startTime).getMonth()}/${new Date(startTime).getFullYear()}`;
  const [hours, minutes, seconds] = totalTime.split(":").map(number => convertToNumber(number));
  const totalTimeInSeconds =  hours*60*60   minutes*60   seconds;
  output[`${startWeek}-${endWeek}`] = (output[`${startWeek}-${endWeek}`] || 0)   totalTimeInSeconds;
});

const preparedOutput = [];
for (const key in output) {
    preparedOutput.push({weekOf: key, totalTime: new Date(output[key] * 1000).toISOString().substr(11, 8)});
}
console.log(preparedOutput);

  •  Tags:  
  • Related