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)