help with condition for adding dates and times

Hello my glide friends. I’m a new Brazilian here on the forum, and I really need some guidance.

I’m currently developing a production system for the company where I work, and I ended up encountering a problem that I can’t solve. My system plans and controls an equipment production line, I have two important columns of information: One with the duration (in hours) of a task, and another with the date and time that this task should be started. To get the sum right, I used the math column with the task duration value, divided by 24 (hours) so that I could have the value in days and simply add the start date, but that’s where my problem comes in.

We have a start time, lunch time, return time from lunch and departure time. When adding the columns I need that the time before the start of work (08:00 am), lunch (12:00 am to 01:00 pm) and Departure (06:00 pm) is not included in the sum, so:

Start date and time: 08/08/2024 08:00 am

Execution Time: 10:00 Hours

Value that Glide Returns:
08/08/2024 06:00 pm

Actual Value Task End: 09/08/2024 09:00 am (Does not include lunch time and non-working hours)

Somebody?

Ok, this is super complex, as date/time problems always are, so I’ll have to explain each case to see if it fits your description.

Thursday 8AM - takes 5 hours

It would take 4 hours from Thursday 8AM to 12PM, then we don’t count the lunch break, and 1 hour left would be from Thursday 1PM to 2PM, so it ends at Thursday 2PM.

Thursday 8AM - takes 8 hours

It would take 4 hours from Thursday 8AM to 12PM, then we don’t count the lunch break, and 4 hours left would be from Thursday 1PM to 5PM, so it ends at Thursday 5PM.

Friday 5PM - takes 9 hours

It would have only 1 hour left at the end of Friday, from Friday 5PM - 6PM. Then it will only be continued on Monday 8PM. We have 8 hours left for Monday, so it would be Monday 8PM - 12PM, and 1PM - 5PM. It ends at Monday 5PM.

Saturday at 7AM - takes 10 hours

Tasks can not start on a weekend, so we push the start time to Monday 8AM. Then we have Monday 8AM - 12PM, and 1PM - 6PM. One hour left will be pushed to Tuesday 8AM - 9AM, so it ends at Tuesday 9AM.

Sunday at 7AM - takes 10 hours

Same calculation as above, because we can’t also startg tasks on Sunday, so it ends at Tuesday 9AM as well.

Monday at 12:30PM - takes 3 hours

We can’t start a task during lunch time, so the start time is pushed to 1PM. Then it’s 1PM - 4PM for a 3-hour task. We end at Monday 4PM.

Tuesday at 7AM - takes 16 hours

We can’t start a task at 7AM, so the start time is pushed to 8PM. Then we have Tuesday 8AM - 12PM, and 1PM - 6PM. That leaves 7 hours left for Wednesday. Wednesday 8AM - 12PM, and 1PM - 4PM would cover it, so it ends at Wednesday 4PM.

1 Like

Set up:

Start Numeral

YEAR(S)*10^8+MONTH(S)*10^6+DAY(S)*10^4+HOUR(S)*10^2+MINUTE(S)

End (JavaScript)

function calculateTaskEnd(startTime, executionHours) {
  // Convert number to string for easier parsing
  const startTimeStr = startTime.toString().padStart(12, '0');
  
  // Parse start time
  const year = parseInt(startTimeStr.slice(0, 4));
  const month = parseInt(startTimeStr.slice(4, 6)) - 1; // JS months are 0-indexed
  const day = parseInt(startTimeStr.slice(6, 8));
  const hours = parseInt(startTimeStr.slice(8, 10));
  const minutes = parseInt(startTimeStr.slice(10, 12));
  
  let currentDate = new Date(year, month, day, hours, minutes);
  
  // Adjust start time to next valid working hour
  currentDate = adjustToWorkingHours(currentDate);
  
  let remainingHours = executionHours;
  
  while (remainingHours > 0) {
    // Add one hour
    currentDate.setHours(currentDate.getHours() + 1);
    
    // Adjust if we've hit non-working hours
    currentDate = adjustToWorkingHours(currentDate);
    
    remainingHours--;
  }
  
  // Convert result back to number format
  const resultStr = 
    currentDate.getFullYear().toString() +
    (currentDate.getMonth() + 1).toString().padStart(2, '0') +
    currentDate.getDate().toString().padStart(2, '0') +
    currentDate.getHours().toString().padStart(2, '0') +
    currentDate.getMinutes().toString().padStart(2, '0');
  
  return parseInt(resultStr);
}

function adjustToWorkingHours(date) {
  while (true) {
    const day = date.getDay();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    
    // If weekend, move to Monday
    if (day === 0 || day === 6) {
      date.setDate(date.getDate() + (8 - day) % 7);
      date.setHours(8, 0, 0, 0);
    }
    // Before work hours
    else if (hours < 8) {
      date.setHours(8, 0, 0, 0);
    }
    // Lunch break
    else if (hours === 12) {
      date.setHours(13, 0, 0, 0);
    }
    // After work hours
    else if (hours >= 18) {
      date.setDate(date.getDate() + 1);
      date.setHours(8, 0, 0, 0);
    }
    // Valid working hour
    else {
      break;
    }
  }
  return date;
}

return calculateTaskEnd(p1, p2)

End Formatted

function formatDate(dateNumber) {
  // Convert number to string and ensure it's 12 digits long
  const dateStr = dateNumber.toString().padStart(12, '0');
  
  // Parse the date string
  const year = parseInt(dateStr.slice(0, 4));
  const month = parseInt(dateStr.slice(4, 6)) - 1; // JS months are 0-indexed
  const day = parseInt(dateStr.slice(6, 8));
  const hour = parseInt(dateStr.slice(8, 10));
  const minute = parseInt(dateStr.slice(10, 12));
  
  // Create a Date object
  const date = new Date(year, month, day, hour, minute);
  
  // Array of day names
  const dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  
  // Array of month names
  const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  
  // Format the date
  const formattedDate = `${dayNames[date.getDay()]}, ${monthNames[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()} at ${formatTime(date)}`;
  
  return formattedDate;
}

// Helper function to format time
function formatTime(date) {
  let hours = date.getHours();
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  return `${hours}:${minutes} ${ampm}`;
}

return formatDate(p1)
2 Likes

I really appreciate it, I’ll test the function and get back to you if it works.

1 Like