date differences

  • Thread starter Thread starter John
  • Start date Start date
J

John

I am having difficulty with subtracting two date/times and
getting the result in the format that will show the number
of days, the hours and the minutes.

My example is:
(3/13/2003 11:00:00 AM) - (3/1/2003 8:00:00 AM) Which
should result in 12 days 3 hours, but only gives 3 hours
as the difference.
[txtEndTime]-[txtStartTime] Both txtEndTime and
txtStartTime are stored as General Date.

I have tried several methods, including Format
([txtEndDate]-1-[txtStartDate], "Short Time"), which give
the correct number of hours and minutes, but does not
account for the number of days.
Any help is appreciated
 
Hi,
Not sure if this is what you want but it will return a string in the format:
12 days 3 hours 0 minutes

Public Function ShowDateDiff(dt1 As Date, dt2 As Date) As String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long

lngMinutes = DateDiff("n", dt2, dt1)

lngDays = lngMinutes / 1440
lngHours = (lngMinutes - (lngDays * 1440)) / 60
lngMinutes = lngMinutes - (lngDays * 1440) - (lngHours * 60)

ShowDateDiff = lngDays & " days " & lngHours & " hours " & lngMinutes & " minutes"

End Function
 
Thanks, it works, but I would like to have the format as
dd hh:mm. With the dd (number of days) to be null if the
date/time difference is less that 23:59 hours:minutes.

The desired format is the default Short time with the
addition of the number of days in front of the
hours/minutes.

What you have shared with me so far is great.
Thanks - J
-----Original Message-----
Hi,
Not sure if this is what you want but it will return a string in the format:
12 days 3 hours 0 minutes

Public Function ShowDateDiff(dt1 As Date, dt2 As Date) As String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long

lngMinutes = DateDiff("n", dt2, dt1)

lngDays = lngMinutes / 1440
lngHours = (lngMinutes - (lngDays * 1440)) / 60
lngMinutes = lngMinutes - (lngDays * 1440) - (lngHours * 60)

ShowDateDiff = lngDays & " days " & lngHours & " hours " & lngMinutes & " minutes"

End Function

--
HTH
Dan Artuso, Access MVP


I am having difficulty with subtracting two date/times and
getting the result in the format that will show the number
of days, the hours and the minutes.

My example is:
(3/13/2003 11:00:00 AM) - (3/1/2003 8:00:00 AM) Which
should result in 12 days 3 hours, but only gives 3 hours
as the difference.
[txtEndTime]-[txtStartTime] Both txtEndTime and
txtStartTime are stored as General Date.

I have tried several methods, including Format
([txtEndDate]-1-[txtStartDate], "Short Time"), which give
the correct number of hours and minutes, but does not
account for the number of days.
Any help is appreciated


.
 
Hi,
All you have to do is alter the format within the function.
To get it to return dd hh:mm, just make this change:
ShowDateDiff = lngDays & " " & lngHours & ":" & lngMintues

Similarily, just add more logic to deal with lngDays being 0.
You have the 3 values to work with: lngDays,lngHours and lngMintues.
You can display them any way you please.

--
HTH
Dan Artuso, Access MVP


John said:
Thanks, it works, but I would like to have the format as
dd hh:mm. With the dd (number of days) to be null if the
date/time difference is less that 23:59 hours:minutes.

The desired format is the default Short time with the
addition of the number of days in front of the
hours/minutes.

What you have shared with me so far is great.
Thanks - J
-----Original Message-----
Hi,
Not sure if this is what you want but it will return a string in the format:
12 days 3 hours 0 minutes

Public Function ShowDateDiff(dt1 As Date, dt2 As Date) As String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long

lngMinutes = DateDiff("n", dt2, dt1)

lngDays = lngMinutes / 1440
lngHours = (lngMinutes - (lngDays * 1440)) / 60
lngMinutes = lngMinutes - (lngDays * 1440) - (lngHours * 60)

ShowDateDiff = lngDays & " days " & lngHours & " hours " & lngMinutes & " minutes"

End Function

--
HTH
Dan Artuso, Access MVP


I am having difficulty with subtracting two date/times and
getting the result in the format that will show the number
of days, the hours and the minutes.

My example is:
(3/13/2003 11:00:00 AM) - (3/1/2003 8:00:00 AM) Which
should result in 12 days 3 hours, but only gives 3 hours
as the difference.
[txtEndTime]-[txtStartTime] Both txtEndTime and
txtStartTime are stored as General Date.

I have tried several methods, including Format
([txtEndDate]-1-[txtStartDate], "Short Time"), which give
the correct number of hours and minutes, but does not
account for the number of days.
Any help is appreciated


.
 
Sorry, forgot about the rounding factor. Tyr this:
lngDays = lngMinutes / 1440
lngHours = Fix((lngMinutes - (lngDays * 1440)) / 60)
lngMinutes = Fix(lngMinutes - (lngDays * 1440) - (lngHours * 60))

--
HTH
Dan Artuso, Access MVP


John said:
Setting the concatenation on the display is good.

The problem that I am having is my startDate is 3/27/2000
3:00:00 PM my endDate is 3/27/2000 3:59:00 PM. The result
that the code is giving me is 0 -1:1, when the result
should be 0 00:59.

If the time difference is longer, say startTime is
2/25/2003 4:00:00 PM and end time is 2/25/2003 7:59:00 PM,
the result is 0 4:-1.

From your expression, I have set dt1 = endDate and dt2 =
startDate. If I reverse these, the result is -1 0:1, so I
think that setting the endDate to dt1 and startDate to dt2
is correct.
Any ideas?
Thanks
-----Original Message-----
Hi,
All you have to do is alter the format within the function.
To get it to return dd hh:mm, just make this change:
ShowDateDiff = lngDays & " " & lngHours & ":" & lngMintues

Similarily, just add more logic to deal with lngDays being 0.
You have the 3 values to work with: lngDays,lngHours and lngMintues.
You can display them any way you please.

--
HTH
Dan Artuso, Access MVP


Thanks, it works, but I would like to have the format as
dd hh:mm. With the dd (number of days) to be null if the
date/time difference is less that 23:59 hours:minutes.

The desired format is the default Short time with the
addition of the number of days in front of the
hours/minutes.

What you have shared with me so far is great.
Thanks - J
-----Original Message-----
Hi,
Not sure if this is what you want but it will return a
string in the format:
12 days 3 hours 0 minutes

Public Function ShowDateDiff(dt1 As Date, dt2 As Date) As
String
Dim lngDays As Long
Dim lngHours As Long
Dim lngMinutes As Long

lngMinutes = DateDiff("n", dt2, dt1)

lngDays = lngMinutes / 1440
lngHours = (lngMinutes - (lngDays * 1440)) / 60
lngMinutes = lngMinutes - (lngDays * 1440) - (lngHours *
60)

ShowDateDiff = lngDays & " days " & lngHours & " hours "
& lngMinutes & " minutes"

End Function

--
HTH
Dan Artuso, Access MVP


I am having difficulty with subtracting two date/times
and
getting the result in the format that will show the
number
of days, the hours and the minutes.

My example is:
(3/13/2003 11:00:00 AM) - (3/1/2003 8:00:00 AM) Which
should result in 12 days 3 hours, but only gives 3 hours
as the difference.
[txtEndTime]-[txtStartTime] Both txtEndTime and
txtStartTime are stored as General Date.

I have tried several methods, including Format
([txtEndDate]-1-[txtStartDate], "Short Time"), which
give
the correct number of hours and minutes, but does not
account for the number of days.
Any help is appreciated


.


.
 
Back
Top