Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Calculate open hours

    hi,
    I have three variables
    - string openTime format hh:mm - eg "08:30"
    - string closeTime format hh:mm eg "17:30"
    - float lunchHours eg 1.5

    Are there any js functions I can use to calculate how many hours the shop is open ?

    thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    You could do it using methods of the Date object, but it's probably actually easier to "roll your own".

    Code:
    var openTime = "08:30";
    var closeTime = "17:30";
    var lunchHours = 1.5;
    
    function convertToHours( timeString )
    {
        var temp = timeString.split(":");
        return Number(temp[0]) + ( Number(temp[1]) / 60 );
    }
    
    var openHours = convertToHours(closeTime) - convertToHours(openTime) - lunchHours;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    bugrake (09-12-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    great - thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •