Web Analytics Made Easy -
StatCounter Calculate open hours - CodingForum

Announcement

Collapse
No announcement yet.

Calculate open hours

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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;
    Be yourself. No one else is as qualified.

    Comment


    • #3
      great - thank you

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎