Home All Groups Group Topic Archive Search About

Time difference calculation

Author
2 Feb 2006 8:23 PM
Paulymon
I entered the following in my immediate window and expected to see a result of
4:00

Why is it showing me :12 ?  I want to subtract the 2 date/times and get 4
hours as the result.

Please help

?format(#2/2/2006 2:35:00 PM# -#2/2/2006 10:35:00 AM#,"[h]:mm")
Show quoteHide quote
:12

Author
2 Feb 2006 8:33 PM
Karl E. Peterson
Paulymon wrote:
> I entered the following in my immediate window and expected to see a
> result of 4:00
>
> Why is it showing me :12 ?  I want to subtract the 2 date/times and
> get 4 hours as the result.
>
> Please help
>
> ?format(#2/2/2006 2:35:00 PM# -#2/2/2006 10:35:00 AM#,"[h]:mm")
>> 12

Huh.  All I get is an error. <shrug>

You might want to look-up the DateDiff function in Help.

?datediff("h", #2/2/2006 2:35:00 PM# , #2/2/2006 10:35:00 AM#)
-4

--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 8:35 PM
Jeff Johnson [MVP: VB]
"Paulymon" <Pauly***@discussions.microsoft.com> wrote in message
news:EC777025-45BC-44CB-9BEE-BD6888DB26BF@microsoft.com...

>I entered the following in my immediate window and expected to see a result
>of
> 4:00
>
> Why is it showing me :12 ?  I want to subtract the 2 date/times and get 4
> hours as the result.

Then use DateDiff().

> Please help
>
> ?format(#2/2/2006 2:35:00 PM# -#2/2/2006 10:35:00 AM#,"[h]:mm")
> :12

mm = two-digit month, not minutes. nn = minutes. Why 12, though? Because the
result of #2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM# is, numerically,
0.166666666671517, and since the integer portion of this number (which
represents the date) is 0, that equates to 1899-12-30, hence the month of
12.
Author
2 Feb 2006 8:50 PM
Paulymon
Thanks for the explanation, how do I get the result (4:00) that I want
though.  I tried using nn and get 0

Show quoteHide quote
"Jeff Johnson [MVP: VB]" wrote:

>
> "Paulymon" <Pauly***@discussions.microsoft.com> wrote in message
> news:EC777025-45BC-44CB-9BEE-BD6888DB26BF@microsoft.com...
>
> >I entered the following in my immediate window and expected to see a result
> >of
> > 4:00
> >
> > Why is it showing me :12 ?  I want to subtract the 2 date/times and get 4
> > hours as the result.
>
> Then use DateDiff().
>
> > Please help
> >
> > ?format(#2/2/2006 2:35:00 PM# -#2/2/2006 10:35:00 AM#,"[h]:mm")
> > :12
>
> mm = two-digit month, not minutes. nn = minutes. Why 12, though? Because the
> result of #2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM# is, numerically,
> 0.166666666671517, and since the integer portion of this number (which
> represents the date) is 0, that equates to 1899-12-30, hence the month of
> 12.
>
>
>
Author
2 Feb 2006 8:58 PM
Karl E. Peterson
Paulymon wrote:
> Thanks for the explanation, how do I get the result (4:00) that I want
> though.  I tried using nn and get 0

?format$(#2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM#, "h:nn")
--
Working without a .NET?
http://classicvb.org/
Author
2 Feb 2006 8:59 PM
Dave
Paulymon wrote:
> Thanks for the explanation, how do I get the result (4:00) that I want
> though.  I tried using nn and get 0
?format(#2/2/2006 2:35:00 PM# -#2/2/2006 10:35:00 AM#,"h:nn")
Author
2 Feb 2006 9:03 PM
Rick Rothstein [MVP - Visual Basic]
> Thanks for the explanation, how do I get the result (4:00) that I want
> though.  I tried using nn and get 0

Convert your subtraction to a date so VB knows what it is...

Format(CDate(#2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM#), "hh:nn")

Of course, in a real program, those two dates would be stored in Date
variables and their subtraction would be stored in a Date variable and that
last Date variable would be used as the first argument.

Rick
Author
2 Feb 2006 9:37 PM
Ken Halter
Show quote Hide quote
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:eD4GhwDKGHA.3728@tk2msftngp13.phx.gbl...
>> Thanks for the explanation, how do I get the result (4:00) that I want
>> though.  I tried using nn and get 0
>
> Convert your subtraction to a date so VB knows what it is...
>
> Format(CDate(#2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM#), "hh:nn")
>
> Of course, in a real program, those two dates would be stored in Date
> variables and their subtraction would be stored in a Date variable and
> that
> last Date variable would be used as the first argument.
>
> Rick

......and might even use the built in DateDiff function to do the math <g>

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
Author
2 Feb 2006 10:04 PM
Rick Rothstein [MVP - Visual Basic]
Show quote Hide quote
> >> Thanks for the explanation, how do I get the result (4:00) that I want
> >> though.  I tried using nn and get 0
> >
> > Convert your subtraction to a date so VB knows what it is...
> >
> > Format(CDate(#2/2/2006 2:35:00 PM# - #2/2/2006 10:35:00 AM#), "hh:nn")
> >
> > Of course, in a real program, those two dates would be stored in Date
> > variables and their subtraction would be stored in a Date variable and
> > that
> > last Date variable would be used as the first argument.
> >
> > Rick
>
> .....and might even use the built in DateDiff function to do the math <g>

But that won't give him both the hours and minutes directly... he would have
do DateDiff for minutes and then calculate the hours and remainder minutes.
No big deal, of course, but I sensed the OP was looking to do it "all in
one".

Rick