DateDiff

1. Member
Join Date
Dec 1969
Posts
48

## DateDiff

Hey-<BR><BR>I have a line in a script that finds the difference between two dates and times. So I feed it this...<BR><BR>T1 = "7/06/2002 09:30"<BR>T2 = "7/06/2002 10:00"<BR>xHour = DateDiff("h", T1, T2)<BR><BR>The problem is that only figures difference in hours, so I have to do it again for minutes...<BR><BR>xMin = DateDiff("m", T1, T2)<BR><BR>So my difference in times is xHour:xMin. Now, this is not my actual code, but this shows my problem. The difference between the two times I posted above is 30 minutes. Correct? The code spits back 1 hour 30 minutes. How do I fix that?? I don&#039;t think you can run DateDiff("hm", T1, T2), unless I&#039;m wrong.<BR><BR>Thanks,<BR>Jeremy#121

2. Senior Member
Join Date
Dec 1969
Posts
623

## RE: DateDiff

totalDiffInMinutes = DateDiff("m", T1, T2)<BR><BR>hourPart = Int( totalDiffInMinutes/60 )<BR>minPart = totalDiffInMinutes MOD 60<BR><BR>timeDiffInMyFormat = hourPart & ":" & minPart<BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Very minor improvement to that...

Instead of<BR> hourPart = Int( totalDiffInMinutes/60 )<BR>do<BR> hourPart = totalDiffInMinutes 60 <BR><BR>The backslash operator (in VB, VBA, and VBScript) does an integer-only divide, discarding any remainder.<BR><BR>Also, if you code it like that, and the difference is 67 minutes, you&#039;ll get an answer of<BR> 1:7<BR>you probabaly want <BR> 1:07<BR>or maybe<BR> 01:07<BR>yes?<BR><BR>If so:<BR><BR>timeDiffInMyFormat = hourPart & ":" & Right("0" & minPart,2)<BR>or<BR>timeDiffInMyFormat = Right("0" & hourPart,2) & ":" & Right("0" & minPart,2)<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
623

## Backslash Operator?

wow, never seen that before... thanks as always

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•