473,414 Members | 1,964 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,414 software developers and data experts.

How do i display differences between dates using 2 datediff results?

153 100+
Hi,

Does anyone know how to display differences between 2 times from 2 different dtpicker displaying results in HH:mm rather than just h or n? I am designing a project for friend so she can enter her start, end, and lunch time in order to calculate total weekly hours worked.

I did have a text box input way of doing until i realised that half an hour was 0.70! Any way of rescuing that way of doing it sounds good.



E.g, I have 15:00 and 17:15, I want label1.caption to display 2hrs 15mins.

Apart from the obvious attempts I have tried the following but the answer for 15:00 17:00 is 2.25

Dim minutesdiv, total1, time1hour, time2hour, total2, time1minute, time2minute, timediff As Variant


Private Sub Command1_Click()
time1hour = DTPicker1.Hour
time1minute = DTPicker1.Minute
time2hour = DTPicker2.Hour
time2minute = DTPicker2.Minute
total1 = time1hour & "." & time1minute
total2 = time2hour & "." & time2minute
timediff = DateDiff("n", total1, total2)
minutesdiv = timediff / 60
Label1.Caption = minutesdiv

End Sub

I have used variant as I am new to Vb, I would change appropriatly if I can get working.

I genuinely have spent about 12 hours on this small issue.
Nov 30 '07 #1
19 3600
lotus18
866 512MB
Hi,

Does anyone know how to display differences between 2 times from 2 different dtpicker displaying results in HH:mm rather than just h or n? I am designing a project for friend so she can enter her start, end, and lunch time in order to calculate total weekly hours worked.

I did have a text box input way of doing until i realised that half an hour was 0.70! Any way of rescuing that way of doing it sounds good.


E.g, I have 15:00 and 17:15, I want label1.caption to display 2hrs 15mins.

Apart from the obvious attempts I have tried the following but the answer for 15:00 17:00 is 2.25

Dim minutesdiv, total1, time1hour, time2hour, total2, time1minute, time2minute, timediff As Variant

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. time1hour = DTPicker1.Hour
  3. time1minute = DTPicker1.Minute
  4. time2hour = DTPicker2.Hour
  5. time2minute = DTPicker2.Minute
  6. total1 = time1hour & "." & time1minute
  7. total2 = time2hour & "." & time2minute
  8. timediff = DateDiff("n", total1, total2)
  9. minutesdiv = timediff / 60
  10. Label1.Caption = minutesdiv
  11.  
  12. End Sub
  13.  
  14.  
I have used variant as I am new to Vb, I would change appropriatly if I can get working.

I genuinely have spent about 12 hours on this small issue.
Hi brendanmcdonagh

Try this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.      Label1.Caption = Format((DTPicker1.Value - DTPicker2.Value), "hh:nn")
  3. End Sub
  4.  
Try to add a validation (e.g. Time1 must higher than Time2) Just continue it...

Hope this helps : )

Rey Sean
Dec 1 '07 #2
brendanmcdonagh
153 100+
You don't know how relieved I am that it works.

I can go to sleep now!

Thank you so much,

Brendan
Dec 1 '07 #3
brendanmcdonagh
153 100+
Hi again,

The code given above works fine unless my friend started at 23:00 and end at say 17:00, it's getting confused I know. So does that mean I need to have custome dtpicker showing date as well as time? How would custom that if I need to?

It really seems to be getting complicated because when I get the answer to the above code, then I'm going to have to add them all up, add lunch amounts in time format and then subtract total of lunch times as well. Primary testing shows again it'll get confused.

Surely People have done this kind of thing before on vb, all I Want to do is add start time, end time, how much for lunch for each day of week and then calculate amount of hours worked.

Any direction would be greatly received (I want to do it my self, I just need a nudge!)
Dec 1 '07 #4
CyberSoftHari
487 Expert 256MB
Nothing complicated, Use DateDiff and pass date and time to get working hours will solve your problem.
Dec 1 '07 #5
brendanmcdonagh
153 100+
Hi CyberSoftHari

The reason i'm trying simple subtraction is I've tried datediff, calculations are coming up wrong for some reason. For instance, a simple code with start being 01:00 and end being 09:15 is giving me the answer (a headache!) 8.25. Now I'm not that good at maths but shouldn't it be 8.15?

Dim time As Double
Dim timediv As Double
Private Sub Command1_Click()
time = DateDiff("n", DTPicker1.Value, DTPicker2.Value)
timediv = time / 60
Label1.Caption = timediv

End Sub
Dec 1 '07 #6
QVeen72
1,445 Expert 1GB
Hi,

Change your Code to :

Expand|Select|Wrap|Line Numbers
  1. Dim time1 As Double
  2. Private Sub Command1_Click()
  3. time1 = DateDiff("n", DTPicker1.Value, DTPicker2.Value)
  4. Label1.Caption = (time1 \ 60)  & ":" & (time1 Mod 60)
  5. End Sub
  6.  
  7. ' Or In Single Statement:
  8.  
  9. Label1.Caption = (DateDiff("n",DTPicker1.Value, DTPicker2.Value) \ 60) & ":" & (DateDiff("n",DTPicker1.Value, DTPicker2.Value) Mod 60)
  10.  
  11.  
Dont Use "Time" as Varaible Name as It is a Reserved Word in VB

Regards
Veena
Dec 1 '07 #7
brendanmcdonagh
153 100+
Hi,

Change your Code to :

Expand|Select|Wrap|Line Numbers
  1. Dim time1 As Double
  2. Private Sub Command1_Click()
  3. time1 = DateDiff("n", DTPicker1.Value, DTPicker2.Value)
  4. Label1.Caption = (time1 \ 60)  & ":" & (time1 Mod 60)
  5. End Sub
  6.  
  7. ' Or In Single Statement:
  8.  
  9. Label1.Caption = (DateDiff("n",DTPicker1.Value, DTPicker2.Value) \ 60) & ":" & (DateDiff("n",DTPicker1.Value, DTPicker2.Value) Mod 60)
  10.  
  11.  
Dont Use "Time" as Varaible Name as It is a Reserved Word in VB

Regards
Veena
Thanks Veena (again!)

I have done the following code instead which shows the same answer -

Lblsunsubtotal.Caption = Format((DTPickersunend.Value - DTPickersunstart.Value), "hh:nn")

lblmonsubtotal.Caption = Format((DTPickermonend.Value - DTPickermonstart.Value), "hh:nn")

Just the same - right?

Any ideas how to add the answers together?
Dec 1 '07 #8
QVeen72
1,445 Expert 1GB
Hi,

Uee the Same Logic :

Expand|Select|Wrap|Line Numbers
  1.  
  2. lblTotal = 
  3. Format((DTPickersunend.Value - DTPickersunstart.Value + DTPickermonend.Value - DTPickermonstart.Value), "hh:nn")
  4.  
  5.  
Regards
Veena
Dec 1 '07 #9
brendanmcdonagh
153 100+
Thank you so much - I hope I can give back to community one day soon!
Dec 1 '07 #10
brendanmcdonagh
153 100+
Veena (or anyone!)

I thought just adding another subtraction for lunch in the calculation would be simple.

What im doing: Taking end time away from start time and then taking away lunch time then I 'm adding result to next calculation for mondays, then tuesday, etc.

The answer is coming up for start at 07:00, end at 19:00 and lunch is 01:00 is 13:00. Itseems to be adding lunch instead of taking it away.

If I can solve this I've created my first program! (with a little help!) ;)

lbltotal = Format((DTPickersunend.Value - DTPickersunstart.Value - DTPickersunlunch.Value + DTPickermonend.Value - DTPickermonstart.Value - DTPickermonlunch.Value + DTPickertuesend.Value - DTPickertuesstart.Value - DTPickertueslunch + DTPickerwedsend.Value - DTPickerwedsstart.Value - DTPickerwedslunch.Value + DTPickerthursend.Value - DTPickerthursstart.Value - DTPickerthurslunch.Value + DTPickerfriend.Value - DTPickerfristart.Value - DTPickerfrilunch.Value + DTPickersatend.Value - DTPickersatstart.Value - DTPickersatlunch.Value), "hh:nn")
Dec 1 '07 #11
QVeen72
1,445 Expert 1GB
Hi,

Your Lunch "13:00" is the Time of Lunch..
But What is the Duration of Lunch..?
As you dont have LunchStart And LunchEnd, you Simply
cannot Subtract Lunch.DatePicker Value..

If Duration is Fixed everyday, say 1 Hr/30 mins, Directly Subtract it....

Regards
Veena
Dec 1 '07 #12
brendanmcdonagh
153 100+
Lunch won't be fixed everyday, So is my only option to have dtpicker for lunch start and end and then somehow enter them into calculation? No chance of having text box input for lunch to take away?
Dec 1 '07 #13
VACEPROGRAMER
168 100+
Hi friend

I have a that program in my book

when i get home i will send you

Thanks for the help

Vace
Dec 1 '07 #14
brendanmcdonagh
153 100+
Hi friend

I have a that program in my book

when i get home i will send you

Thanks for the help

Vace
Thanks Vace, I appreciate it :)
Dec 1 '07 #15
VACEPROGRAMER
168 100+
Dont wory
Lately I will send you tomorow

Dont wory my FRINED

By the way Thanks for the help

Do you want to get in my team a?
Dec 1 '07 #16
QVeen72
1,445 Expert 1GB
Lunch won't be fixed everyday, So is my only option to have dtpicker for lunch start and end and then somehow enter them into calculation?
Hi,

Yes, Thats better Have one More DatePicker for LunchStart and LunchEnd,
Then WorkHours PerDay will be:

DayEnd - DayStart -(LunchEnd - LunchStart)

Regards
Veena
Dec 2 '07 #17
brendanmcdonagh
153 100+
Thanks Veena,

Any idea how i can stop it resetting at 24 hours. e.g. 25:00 is how i want it to be displayed but it'll display currently 01:00. It's to do with hh:nn custom format ------- .Value + DTPickerfriend.Value - DTPickerfristart.Value - DTPickerfrilunch.Value + DTPickersatend.Value - DTPickersatstart.Value - DTPickersatlunch.Value), "hh:nn")

I have asked a few questions this weekend but have learnt so much!
Dec 2 '07 #18
QVeen72
1,445 Expert 1GB
Hi,

For time Formats, you cannot make Entry like "25:00"..
What you can do is :
Format DatPicker for Both Date and Time
Or
Use TextBox, with proper Validations.(Coding required)

First Option will be Good.

Regards
Veena
Dec 3 '07 #19
VACEPROGRAMER
168 100+
Sorry I'm late. I have found the program you want and I make somewhone with same function. I'll send you abaout 2 or 3 hours

Vace
Dec 3 '07 #20

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

Similar topics

1
by: Jesse O | last post by:
I have two date fields, start_date and end_date. I'd like to subtract the two dates, and come up with a number (the number of difference between the two dates). What function is there to do...
19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
4
by: Dean | last post by:
I am a developer who works with MS SQL Server a lot, but I'm far from an expert. I am revamping an appointment scheduling system that allows for appointments to reoccur daily, weekly, monthly and...
8
by: Mojca | last post by:
What is formula that get days between two dates? Npr: 11.03.1998 – 1.7.2005 Thank you, Mojca
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
2
by: Paul Aspinall | last post by:
Hi I want to calculate the difference between 2 dates in C#. I know there is a function in VB, called DateDiff, but I don't want to ref the VB library, and want to try to do it natively in C#. ...
3
by: CDMAPoster | last post by:
A.K.A. Is Double Dating a bad thing :-)? My post from several hours ago may have gotten lost so please forgive me if something similar to this shows up twice. From a modular programming class I...
1
by: Don G | last post by:
Each entry in a data file includes date and time in text format - e.g. "0601271325" = 2006, January 27th, 1:25 pm. Is there a simple way to calculate the hours (including decimal parts) between...
18
by: mlcampeau | last post by:
I have a lengthy query that I am now trying to filter. The query calculates an employee's Anniversary Date in which they are eligible for the next level of Annual Vacation. (i.e. For 1-6 years of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.