<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Job Done with LuxonJS</title>
<meta name="viewport" content="width=device-width">
<style>
#output {
font-size: 2rem;
}
#output2 {
font-size: 2rem;
/* color: red; */
}
#output3 {
font-size: 2rem;
color: green;
}
</style>
</head>
<body>
<header>
<h1>Calculating Completion Date Time</h1>
</header>
<main>
<div id="output"></div>
<div id="output2"></div>
<div id="output3"></div>
</main>
<!-- <script src="luxon.min.js"></script>-->
<script src="https://cdn.jsdelivr.net/npm/luxon@1.24.1/build/global/luxon.min.js"></script>
<script>
Number.prototype.between = function(a, b, inclusive) { //For elegant If statment
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return inclusive ? this >= min && this <= max : this > min && this < max;
};
console.clear();
//weekday: number Get the day of the week. 1 is Monday and 7 is Sunday
var OpenClose = [{}, // elemnt 0 not used to make Luxon Day Of the Week the index into the array
{
dw: 'Monday',
open: 4,
close: 22,
LuxonDayOfWeek: 1
},
{
dw: 'Tuesday',
open: 4,
close: 22,
LuxonDayOfWeek: 2
},
{
dw: 'Wedneday',
open: 4,
close: 22,
LuxonDayOfWeek: 3
},
{
dw: 'Thursday',
open: 4,
close: 22,
LuxonDayOfWeek: 4
},
{
dw: 'Friday',
open: 4,
close: 19,
LuxonDayOfWeek: 5
},
{
dw: 'Saturday',
open: 6,
close: 18,
LuxonDayOfWeek: 6
},
{
dw: 'Sunday',
open: 13,
close: 22,
LuxonDayOfWeek: 7
}
]; //orderby
console.log(OpenClose); //Show the Shop Schedule
let DateTime = luxon.DateTime;
var local = DateTime.local(); //Now here
//Make changes here !!
let Remainghours = 12.15; //Time alotted for the job. Change to test other values.
var NowInPT = local.setZone("America/Los_Angeles"); //Seattle is in this TimeZone
let JobDonePT = NowInPT; //Start at same time
output.textContent = "Starting in PT on " + NowInPT.toLocaleString(DateTime.DATETIME_SHORT) + " Job takes " + Remainghours + " hours";
while (Remainghours > 0) {
if (JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) {
console.log("Shop Open: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
Remainghours--; //Shop is open Use up an hour
} else {
console.log("Shop Closed: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
//keep going without Using Remainghours
}
JobDonePT = JobDonePT.plus({
hours: 1
}); //advance 1 hour on the calendar in any case
}
// Now we are left with a Negative fraction of an hour so we set set the actual Completion
JobDonePT = JobDonePT.plus({
hours: Remainghours
}); //Remainghours is negative
//The end DateTime may still be in Off hours Must end In work hours so
while (!JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) { //Not working hours
debugger;
JobDonePT = JobDonePT.plus({
hours: 1
});
};
output2.textContent = "Job End in PT on " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT);
var JobDoneMylocal = JobDonePT.toLocal();
output3.textContent = "Job End in My Time Zone on " + JobDoneMylocal.toLocaleString(DateTime.DATETIME_SHORT);
</script>
</body>
</html>