Calculate how much of a N hour segment is outside business hours 8AM -> 9PM












0















I need a function that given two dates A and B which are always N hours apart (3 or 4, but anything under 10), gives me the total number of hours that fall outside of a window 8AM->5PM (or, 800 -> 1700). You can assume that the window is always small enough so that it can only start before 8AM OR end after 5PM. However, I would love to see something powerful enough to consider both cases.



I am developing an overtime calculator for an invoice, and was given this requirement.



I started playing with getHours() to see what the hour is on date A, and if it's less than 5, calculate the number of hours difference by creating a new date object with A5 = new Date(date.getTime()) and then A5.setHours(5) and then Math.abs(A - A5).



If A is more than 5, then I do the same thing but considering 8 the cutoff time.



I would need to use getHours() to check if the segment A -> B is TOTALLY outside of the 8AM->5PM window (in which case, return 0).



But... what if the window is 10 hours, 11:00PM to 9:00AM? The first IF would not happen as 11 > 5; the second IF wouldn't either, because 9 < 17.



Surely there are established, elegant ways of doing this?



Simple attempt:



function c () {
// var D = 1542695151752 // 14:31
// var D = 1542684151000 // 11:22
// var D = 1542645151000 // 00:32
var D = 1542667151000 // 6:39

var S = 8
var E = 17

var A = new Date(D)
var B = new Date(D + 1000 * 60 * 60 * 4)

var Ah = A.getHours()
var Bh = B.getHours()

var NE
var NS

if (Ah >= S && Ah <= E && Bh >= S && Bh <= E) return (Math.abs(B - A)) / 1000 / 60 / 60
if ((Ah < S || Ah > E) && (Bh < S || Bh > E)) return (0)

if (Ah >= S && Ah <= E) {
NS = A
NE = new Date(B.getTime())
NE.setHours(E)
NE.setMinutes(0)
NE.setSeconds(0)
}

if (Bh >= S && Bh <= E) {
NE = B
NS = new Date(A.getTime())
NS.setHours(S)
NS.setMinutes(0)
NS.setSeconds(0)
}
return (Math.abs(NS - NE) / 1000 / 60)
}

console.log(c())









share|improve this question

























  • This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

    – Merc
    Nov 20 '18 at 6:22






  • 1





    looks like a good time to use moment.js

    – charlietfl
    Nov 20 '18 at 6:43











  • Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

    – cd-rum
    Nov 20 '18 at 6:58











  • Happy to use moment! But... does it do what I need?

    – Merc
    Nov 20 '18 at 7:07











  • It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

    – Merc
    Nov 21 '18 at 0:08
















0















I need a function that given two dates A and B which are always N hours apart (3 or 4, but anything under 10), gives me the total number of hours that fall outside of a window 8AM->5PM (or, 800 -> 1700). You can assume that the window is always small enough so that it can only start before 8AM OR end after 5PM. However, I would love to see something powerful enough to consider both cases.



I am developing an overtime calculator for an invoice, and was given this requirement.



I started playing with getHours() to see what the hour is on date A, and if it's less than 5, calculate the number of hours difference by creating a new date object with A5 = new Date(date.getTime()) and then A5.setHours(5) and then Math.abs(A - A5).



If A is more than 5, then I do the same thing but considering 8 the cutoff time.



I would need to use getHours() to check if the segment A -> B is TOTALLY outside of the 8AM->5PM window (in which case, return 0).



But... what if the window is 10 hours, 11:00PM to 9:00AM? The first IF would not happen as 11 > 5; the second IF wouldn't either, because 9 < 17.



Surely there are established, elegant ways of doing this?



Simple attempt:



function c () {
// var D = 1542695151752 // 14:31
// var D = 1542684151000 // 11:22
// var D = 1542645151000 // 00:32
var D = 1542667151000 // 6:39

var S = 8
var E = 17

var A = new Date(D)
var B = new Date(D + 1000 * 60 * 60 * 4)

var Ah = A.getHours()
var Bh = B.getHours()

var NE
var NS

if (Ah >= S && Ah <= E && Bh >= S && Bh <= E) return (Math.abs(B - A)) / 1000 / 60 / 60
if ((Ah < S || Ah > E) && (Bh < S || Bh > E)) return (0)

if (Ah >= S && Ah <= E) {
NS = A
NE = new Date(B.getTime())
NE.setHours(E)
NE.setMinutes(0)
NE.setSeconds(0)
}

if (Bh >= S && Bh <= E) {
NE = B
NS = new Date(A.getTime())
NS.setHours(S)
NS.setMinutes(0)
NS.setSeconds(0)
}
return (Math.abs(NS - NE) / 1000 / 60)
}

console.log(c())









share|improve this question

























  • This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

    – Merc
    Nov 20 '18 at 6:22






  • 1





    looks like a good time to use moment.js

    – charlietfl
    Nov 20 '18 at 6:43











  • Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

    – cd-rum
    Nov 20 '18 at 6:58











  • Happy to use moment! But... does it do what I need?

    – Merc
    Nov 20 '18 at 7:07











  • It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

    – Merc
    Nov 21 '18 at 0:08














0












0








0








I need a function that given two dates A and B which are always N hours apart (3 or 4, but anything under 10), gives me the total number of hours that fall outside of a window 8AM->5PM (or, 800 -> 1700). You can assume that the window is always small enough so that it can only start before 8AM OR end after 5PM. However, I would love to see something powerful enough to consider both cases.



I am developing an overtime calculator for an invoice, and was given this requirement.



I started playing with getHours() to see what the hour is on date A, and if it's less than 5, calculate the number of hours difference by creating a new date object with A5 = new Date(date.getTime()) and then A5.setHours(5) and then Math.abs(A - A5).



If A is more than 5, then I do the same thing but considering 8 the cutoff time.



I would need to use getHours() to check if the segment A -> B is TOTALLY outside of the 8AM->5PM window (in which case, return 0).



But... what if the window is 10 hours, 11:00PM to 9:00AM? The first IF would not happen as 11 > 5; the second IF wouldn't either, because 9 < 17.



Surely there are established, elegant ways of doing this?



Simple attempt:



function c () {
// var D = 1542695151752 // 14:31
// var D = 1542684151000 // 11:22
// var D = 1542645151000 // 00:32
var D = 1542667151000 // 6:39

var S = 8
var E = 17

var A = new Date(D)
var B = new Date(D + 1000 * 60 * 60 * 4)

var Ah = A.getHours()
var Bh = B.getHours()

var NE
var NS

if (Ah >= S && Ah <= E && Bh >= S && Bh <= E) return (Math.abs(B - A)) / 1000 / 60 / 60
if ((Ah < S || Ah > E) && (Bh < S || Bh > E)) return (0)

if (Ah >= S && Ah <= E) {
NS = A
NE = new Date(B.getTime())
NE.setHours(E)
NE.setMinutes(0)
NE.setSeconds(0)
}

if (Bh >= S && Bh <= E) {
NE = B
NS = new Date(A.getTime())
NS.setHours(S)
NS.setMinutes(0)
NS.setSeconds(0)
}
return (Math.abs(NS - NE) / 1000 / 60)
}

console.log(c())









share|improve this question
















I need a function that given two dates A and B which are always N hours apart (3 or 4, but anything under 10), gives me the total number of hours that fall outside of a window 8AM->5PM (or, 800 -> 1700). You can assume that the window is always small enough so that it can only start before 8AM OR end after 5PM. However, I would love to see something powerful enough to consider both cases.



I am developing an overtime calculator for an invoice, and was given this requirement.



I started playing with getHours() to see what the hour is on date A, and if it's less than 5, calculate the number of hours difference by creating a new date object with A5 = new Date(date.getTime()) and then A5.setHours(5) and then Math.abs(A - A5).



If A is more than 5, then I do the same thing but considering 8 the cutoff time.



I would need to use getHours() to check if the segment A -> B is TOTALLY outside of the 8AM->5PM window (in which case, return 0).



But... what if the window is 10 hours, 11:00PM to 9:00AM? The first IF would not happen as 11 > 5; the second IF wouldn't either, because 9 < 17.



Surely there are established, elegant ways of doing this?



Simple attempt:



function c () {
// var D = 1542695151752 // 14:31
// var D = 1542684151000 // 11:22
// var D = 1542645151000 // 00:32
var D = 1542667151000 // 6:39

var S = 8
var E = 17

var A = new Date(D)
var B = new Date(D + 1000 * 60 * 60 * 4)

var Ah = A.getHours()
var Bh = B.getHours()

var NE
var NS

if (Ah >= S && Ah <= E && Bh >= S && Bh <= E) return (Math.abs(B - A)) / 1000 / 60 / 60
if ((Ah < S || Ah > E) && (Bh < S || Bh > E)) return (0)

if (Ah >= S && Ah <= E) {
NS = A
NE = new Date(B.getTime())
NE.setHours(E)
NE.setMinutes(0)
NE.setSeconds(0)
}

if (Bh >= S && Bh <= E) {
NE = B
NS = new Date(A.getTime())
NS.setHours(S)
NS.setMinutes(0)
NS.setSeconds(0)
}
return (Math.abs(NS - NE) / 1000 / 60)
}

console.log(c())






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:08







Merc

















asked Nov 20 '18 at 6:10









MercMerc

7,007114789




7,007114789













  • This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

    – Merc
    Nov 20 '18 at 6:22






  • 1





    looks like a good time to use moment.js

    – charlietfl
    Nov 20 '18 at 6:43











  • Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

    – cd-rum
    Nov 20 '18 at 6:58











  • Happy to use moment! But... does it do what I need?

    – Merc
    Nov 20 '18 at 7:07











  • It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

    – Merc
    Nov 21 '18 at 0:08



















  • This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

    – Merc
    Nov 20 '18 at 6:22






  • 1





    looks like a good time to use moment.js

    – charlietfl
    Nov 20 '18 at 6:43











  • Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

    – cd-rum
    Nov 20 '18 at 6:58











  • Happy to use moment! But... does it do what I need?

    – Merc
    Nov 20 '18 at 7:07











  • It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

    – Merc
    Nov 21 '18 at 0:08

















This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

– Merc
Nov 20 '18 at 6:22





This is such a basic question and yet I am going insane with it... grrrrrrr. I know there is a super-easy solution. I know it.

– Merc
Nov 20 '18 at 6:22




1




1





looks like a good time to use moment.js

– charlietfl
Nov 20 '18 at 6:43





looks like a good time to use moment.js

– charlietfl
Nov 20 '18 at 6:43













Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

– cd-rum
Nov 20 '18 at 6:58





Yeah, do not reinvent the wheel here. Use moment.js. Relevant xkcd, even if zones aren't your concern right now.

– cd-rum
Nov 20 '18 at 6:58













Happy to use moment! But... does it do what I need?

– Merc
Nov 20 '18 at 7:07





Happy to use moment! But... does it do what I need?

– Merc
Nov 20 '18 at 7:07













It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

– Merc
Nov 21 '18 at 0:08





It's all fun games saying "Don't reinvent the wheel! Use moment!" And provide zero information about how to actually do so.

– Merc
Nov 21 '18 at 0:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387198%2fcalculate-how-much-of-a-n-hour-segment-is-outside-business-hours-8am-9pm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387198%2fcalculate-how-much-of-a-n-hour-segment-is-outside-business-hours-8am-9pm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini