473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to calculate the number of working hours in Week excluding Weekends in JavaScript

please suggest how to code the below scenario with javascript or jquery. I am trying to code a calculator that calculates the time between two dates excluding weekends i.e a task comes to the desk on Friday at 12.00am and ends on coming Tuesday 08:00 am. it should calculate the time between Friday and Tuesday excluding Saturday and Sunday. it should give o/p 56 hrs
Oct 12 '17 #1
1 3891
Sherin
77 64KB
This will work:

Expand|Select|Wrap|Line Numbers
  1. var calcWorkHours = function calcWorkHours(start, end) {
  2.         var total = (end - start) / (1000 * 60 * 60 * 24),  // compute difference in days
  3.             totalDays = parseInt(total, 10),                // trim fractional portion
  4.             remainder = (total - totalDays) * 24,           // save fractional portion in hours
  5.             startingDay = start.getDay(),                   // starting weekday
  6.             endingDay = end.getDay(),                       // ending weekday
  7.             possibleWorkingDays = (totalDays * 5),
  8.             actualOccurringWeekends = (startingDay - endingDay) * 2,
  9.             workDays = (possibleWorkingDays - actualOccurringWeekends) / 7;
  10.         if (endingDay === 6) {
  11.             workDays -= 1;  // ends on a weekend, subtract a day
  12.         }
  13.         if (startingDay === 0) {
  14.             workDays -= 1;  // starts on a weekend, subtract a day
  15.         }
  16.         return (workDays * 24) + remainder; // convert to hours and add back fractional portion
  17.     },
  18.     parseInputs = function parseInputs(date, hour, minute) {
  19.         var dmy = date.split("/"),
  20.             now = new Date(),
  21.             thisMinute = now.getMonth() + 1 + "-" + now.getDate() + "-" + now.getFullYear() + " " + now.getHours() + ":" + now.getMinutes() + ":00",
  22.             start = Date.parse(dmy[1] + "-" + dmy[0] + "-" + dmy[2] + " " + hour + ":" + minute + ":00"),
  23.             end = Date.parse(thisMinute);
  24.         start = new Date(start);
  25.         end = new Date(end);
  26.         return calcWorkHours(end, start);
  27.     };
Mar 13 '20 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: | last post by:
Hello, I am hoping someone else has thought about a date time calculation i need to perform. I would like to be able to calculate the number of "working minutes" between 2 dates, given my...
4
by: Totto | last post by:
Hi, Is ther a simple way to calculate number of hours in a date range. (dtFrom as date , dtTo as date) Had a look at timespan, but can's see that it's possible to add dates. Tnx Totto
0
by: seyed | last post by:
how to get the difference between two dates ,excluding weekends in ms sql? i using asp.net , if u all noe any other solution , please reply
1
by: deena22 | last post by:
hello, i'm using 'Access database' and VB 6.0. My database is named ' timesheet' and it contains a table named 'tabletimesheet'. The table contain the following fields: 'staffname, stafftype,...
9
by: brendanmcdonagh | last post by:
Hi, I am designing a project for my friend as I am just a week old baby to vb and want to implement what I've learnt so far. She wants to be able to put start time, end time and lunch for each...
10
bsmeena7005
by: bsmeena7005 | last post by:
Hi help me to calculate the working hours between 10 to 18 hrs between two dates including the weekends also no braek or lunch time Example :- start date #01-08-2009 08:45:23# end date ...
0
by: nilanjangm | last post by:
I am using Access 2007. In a table, I have two columns say Start_Date and End_Date of datatype Date/Time (Short Date). Now, I want to calculate the duration of work in that period assuming 8 hours...
9
by: ssoni | last post by:
hi, I am new to sq server, and I want to calculate the total working hours for all the employees excluding their break. This is the sample table for 2 employes. logID logDate logTime...
1
by: wirejp | last post by:
Hi All, I am using MS Access 2010. I am working out each member of staffs working hours for the month in a MS Access query. minus a 1 hour lunch. I would like to display the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.