Subtracting two dates (including hours and minutes) considering only working time vba or Excel - vba

I need to subtract two dates (including hours and minutes), but I only need to consider working hours. That is, I need to omit lunch time (from 13 to 14 hrs), weekends and hours after 18 hrs and before 9 hrs of the following day, in a working day (from Mo to Fr). Any thoughts?
I don't mind if it's an Excel formula or a vba code.
I have this formula, but it doesn't omit lunch time:
9*(NETWORKDAYS(initial_time;ending_time)-1)-24*((MOD(initial_time;1)-MOD(ending_‌​time;1)))

Here's a possible solution. It assumes an 8 hour work days for all but the start and end date. Also that start date/time is 9:00 or after and end date/time is 18:00 or earlier and that both are on a weekday.
=(NETWORKDAYS(A2,B2)-2)*8+IF(MOD(A2,1)>0.58333,(TIME(18,0,0)-MOD(A2,1))*24,(TIME(18,0,0)-MOD(A2,1))*24-1)+IF(MOD(B2,1)>0.58333,(MOD(B2,1)-TIME(9,0,0))*24-1,(MOD(B2,1)-TIME(9,0,0))*24)
.58333 equates to 14:00. The formula:
multiplies networkdays * 8
+ hours from start date/time until 18:00 subtracting 1 hour if start time is before 14:00
+ hours from 9:00 until end date/time subtracting 1 hour if end time is after 14:00
Of course this doesn't take any holidays into account.

Related

Date_diff with specific condition time start and time end

is it possible to have date_diff with specific start and end time?
let say my store are open from 8AM - 10PM, which is 14 Hours.
and I have a lot of stuff to sell during that time. One of the SKU is out of stock from 2022-11-01 06.00 PM until tomorrow 2022-11-02 11.00 AM.
Instead of calculate 24 hours, I just want to calculate only from opening store until it closed or until its restock. Meaning from 6PM to 11AM is 8 Hours
my query
select date_diff('2022-11-02 11.00 AM', '2022-11-02 06.00 PM', hour) from table
with the result 17 hours instead of 8 hours
There isn't a way to configure DATE_DIFF to do this for you, but it's possible to do what you want, with some effort.
You should convert your dates to timestamps (TIMESTAMP(yourdate) or CAST(yourdate AS TIMESTAMP)) and use TIMESTAMP_DIFF instead.
This will allow you to work with smaller intervals than days.
For your calculation, you ultimately need to find the total time difference between the two timestamps and then subtract the out-of-hours timeframe.
However, calculating the latter is not as simple as taking the difference in days and multiplying by 8 hours (10pm-6am), because your out-of-hours calculation has to account for weekends and possibly holidays etc. Hence it can get quite complex, which is where the solution in my first link might come in.

shift out time taking as next date in time in excel

I am a admin of a bio-metric device, there are 3 shifts are going in my office.
In time Out time Date
Shift1 6AM 2PM 5th April
Shift2 2PM 10PM 5th April
Shift3 10PM 5th April 6AM 6th April
but the 3rd shift time intime 10PM is taking as
In time Out Time
day1 out time 10PM
day2 in time 6AM
How can I make it for same date 10PM as intime and 6AM as out time in excel?
And also I want to generate it automatically.
I want the Out Time must shown as the same date.
Date Intime Outtime
5.4.17 6AM 2PM
5.4.17 2PM 10PM
5.4.17 10PM 6AM
And I want to automate the report while generating from web.
=Now() will generate a number (double type) like 42832.8008217593 where the date is shown on the left of the period and the time on the right. 10 PM (22:00:00) will be expressed as 0.9166666667. Add 8 hours to this number (1/24*8) and the result is 1.25 which means, in plain language, "next day 6 AM".
To solve your problem you can use a condition, like, If the end time is on the next day, deduct one day from the result date. This would mean 1.25 - 1 = 0.25 = 6:00AM on the day which is one day earlier than it actually is. Whether your end time is a proper date/time, like 42833.25 or a relative time like 1.25 depends upon how you enter your data.
You can extract the time from the date/time value by calculating (Date/Time - Int(Date/Time), like 1.25 - Int(1.25) = 1.25 -1 = 0.25

How to add working hours depending on weekday in sql

I want to create a basic case logging system and when somebody opens a new issue, the issue is assigned a Sr_number with a given number of hours. For example Sr_number 1 is 4 hours, 2 is 6 hours, 3 is 8 hours and 4 is 24 hours.
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 09:00 to 17:00 Monday to Friday.So if a case is given a 12 hour Sr_number and the deadline for this falls at 16:00 on a week day then the deadline is extended to the next working day. Basically the deadline is 12 working hours.And calculation should be 1 hour worked for the issue logged on same day and remaining 11 hours to next working day.
If in case it is sun, it should consider directly go to monday.
Example:
Case created on: 10/06/2015 12:04:39 PM- with Sr_number 1 (12 Hours) Deadline is now: 10/07/2015 12.05 PM
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours.
For some case,saturdays is working ,for some its holiday.
How should i proceed.
I tried performing datepart,dateadd and datediff functions.But i could find only weekday.
I am new at sql.Please guide for the same

datetime manipulation: replace all dates with 00:00 time with 24:00 the previous day

I have a table described here: http://sqlfiddle.com/#!3/f8852/3
The date_time field for when the time is 00:00 is wrong. For example:
5/24/2013 00:00
This should really be:
5/23/2013 24:00
So hour 00:00 corresponds to the last hour of the previous day (I didn't create this table but have to work with it). Is there way quick way when I do a select I can replace all dates with 00:00 as the time with 24:00 the previous day? I can do it easily in python in a for loop but not quite sure how to structure it in sql. Appreciate the help.
All datetimes are instants in time, not spans of a finite length, and they can exist in only one day. The instant that represents Midnight is by definition, in the next day, the day in which it is the start of the day, i.e., a day is closed on its beginning and open at its end, or, to phrase it again, valid allowable time values within a single calendar date vary from 00:00:00.00000, to 23:59:59.9999.
This would be analogous to asking that the minute value within an hour be allowed to vary from 1 to 60, instead of from 0 to 59, and that the value of 60 was the last minute of the previous hour.
What you are talking about is only a display issue. Even if you could enter a date as 1 Jan 2013 24:00, (24:00:00 is not a legal time of day) it would be entered as a datetime at the start of the date 2 Jan, not at the end of 1 Jan.
One thing that illustrates this, is to notice that, because of rounding (SQL can only resolve datetimes to within about 300 milleseconds), if you create a datetime that is only a few milleseconds before midnight, it will round up to midnight and move to the next day, as can be seen by running the following in enterprise manager...
Select cast ('1 Jan 2013 23:59:59.999' as datetime)
SQL server stoers all datetimes as two integers, one that represents the number days since 1 Jan 1900, and the other the number of ticks (1 tick is 1/300th of a second, about 3.33 ms), since midnight. If it has been zero time interval since Midnight, it is stll the same day, not the previous day.
If you have been inserting data assuming that midnight 00:00:00 means the end of the day, you need to fix that.
If you need to correct your existing data, you need to add one day to every date in your database that has midnight as it's time component, (i.e., has a zero time component).
Update tbale set
date_time = dateAdd(day, 1, date_time)
Where date_time = dateadd(day, datediff(day, 0, date_time), 0)

Check and calulate time period between datetime in sql 2005

I need to check if any work time period is between datetime and calculate hours between for work hours sum grater than 8 hours.
Sample data:
1. Work time: 07:00 - 17:00 and datetime for checking 06:00-22:00
Answer: 2 hour
2. Work time: 13:00 - 23:00 and datetime for checking 06:00-22:00
Answer: 1 hour (only 1 hour is grater 8 hours and between 06:00-22:00 )
3. Work time: 19:00 - 05:00 and datetime for checking 22:00-06:00
Answer: 2 hour (only 2 hour is grater 8 hours and between 22:00-06:00 )
Any ideas?
Try this: T-SQL DateDiff - partition by "full hours ago", rather than "times minutes turned 00 since"
Basically, DATEDIFF(HOUR, endTime, startTime). Then, subtract your result from 8 to get the difference (if they were "under" time, the difference will be negative).