This works but I am looking for a cleaner way to write it. Also is there a
way to display the seconds as well?

a = "4/28/06 11:00:00AM"

If (Index = 2) Then
b = Day(a) - Day(Now())
c = Hour(a) - Hour(Now())
d = Minute(a) - Minute(Now())

If (c < 0) Then
c = c + 24
b = b - 1
End If

If (d < 0) Then
d = d + 60
c = c - 1
End If

MsgBox "Days " & b & ", Hours " & c & ", Minutes " & d
End If

Re: Cleaner Way to write this? by Dan

Dan
Tue Apr 11 13:46:13 CDT 2006

What's the point of the routine? Just a date difference (in which case
you could use DateDiff)

Re: Cleaner Way to write this? by Yogi_Bear_79

Yogi_Bear_79
Tue Apr 11 14:21:27 CDT 2006


"Dan" <NoSpam@NoSpam.NoSpam> wrote in message
news:%2322G2gZXGHA.4148@TK2MSFTNGP03.phx.gbl...
> What's the point of the routine? Just a date difference (in which case
> you could use DateDiff)


Point is to retun the exact days, hours, minutes and seconds between current
system time, and a supplied variable.

It is used in a PowerPoint Module so will be VBA. I'll look over DateDiff to
see if it returns the same results.



Re: Cleaner Way to write this? by ekkehard

ekkehard
Tue Apr 11 14:40:03 CDT 2006

Yogi_Bear_79 wrote:

> This works but I am looking for a cleaner way to write it. Also is there a
> way to display the seconds as well?
>
> a = "4/28/06 11:00:00AM"
>
> If (Index = 2) Then
> b = Day(a) - Day(Now())
> c = Hour(a) - Hour(Now())
> d = Minute(a) - Minute(Now())
>
> If (c < 0) Then
> c = c + 24
> b = b - 1
> End If
>
> If (d < 0) Then
> d = d + 60
> c = c - 1
> End If
>
> MsgBox "Days " & b & ", Hours " & c & ", Minutes " & d
> End If
>
>
I'd experiment with something like this:

Dim dtNow : dtNow = Now
Dim aTests : aTests = Array( dtNow _
, DateAdd( "d", -1, dtNow ) _
, DateAdd( "h", -1, dtNow ) _
, DateAdd( "n", -120, dtNow ) _
, DateAdd( "s", -123456, dtNow ) _
, #1/1/2006 01:02:03# _
)
Dim dtTest, sOtp, nDDiff, dtDiff

For Each dtTest In aTests
sOtp = CStr( dtTest ) + " ==> " + CStr( dtNow ) + ": "
nDDiff = DateDiff( "d", dtTest, dtNow )
dtTest = DateAdd( "d", nDDiff, dtTest )
dtDiff = CDate( dtNow - dtTest )
sOtp = sOtp + Right( Space( 5 ) & nDDiff, 5 ) + " Day(s), " & Right( CStr( dtDiff
), 8 )
WScript.Echo sOtp
Next

Re: Cleaner Way to write this? by Mythran

Mythran
Tue Apr 11 15:27:31 CDT 2006


"Yogi_Bear_79" <nospam@spamsux.com> wrote in message
news:OfydnfSVItbiaabZnZ2dnUVZ_umdnZ2d@comcast.com...
> This works but I am looking for a cleaner way to write it. Also is there a
> way to display the seconds as well?
>
> a = "4/28/06 11:00:00AM"
>
> If (Index = 2) Then
> b = Day(a) - Day(Now())
> c = Hour(a) - Hour(Now())
> d = Minute(a) - Minute(Now())
>
> If (c < 0) Then
> c = c + 24
> b = b - 1
> End If
>
> If (d < 0) Then
> d = d + 60
> c = c - 1
> End If
>
> MsgBox "Days " & b & ", Hours " & c & ", Minutes " & d
> End If
>
>

Don't know about cleaner :) But looks cool! :P

HTH,
Mythran

Option Explicit

WScript.Echo Diff("4/28/06 11:00:00AM")

Function Diff(ByVal Text)
Dim inDate
Dim curDate
Dim days
Dim hours
Dim minutes

If Not IsDate(Text) Then
Err.Raise 1, "Diff", "The specified Text is not a date."
End If

curDate = Now()
inDate = CDate(Text)

days = Day(inDate) - Day(Now())
hours = Hour(inDate) - Hour(Now())
minutes = Minute(inDate) - Minute(Now())

' Calculate days.
days = IIf(hours < 0, days - 1, days)

' Calculate hours.
hours = IIf(hours < 0, hours + 24, hours)
hours = IIf(minutes < 0, hours - 1, hours)

' Calculate minutes.
minutes = IIf(minutes < 0, minutes + 60, minutes)

Diff = "Days " & days & ", Hours " & hours & ", Minutes " & minutes
End Function

Function IIf(Condition, TrueValue, FalseValue)
If Condition Then
IIf = TrueValue
Else
IIf = FalseValue
End If
End Function


Re: Cleaner Way to write this? by Michael

Michael
Wed Apr 12 07:00:44 CDT 2006

On Tue, 11 Apr 2006 15:17:37 -0400, Yogi_Bear_79 wrote in
microsoft.public.scripting.vbscript:

>"Dan" <NoSpam@NoSpam.NoSpam> wrote in message
>news:%2322G2gZXGHA.4148@TK2MSFTNGP03.phx.gbl...
>> What's the point of the routine? Just a date difference (in which case
>> you could use DateDiff)
>
>
>Point is to retun the exact days, hours, minutes and seconds between current
>system time, and a supplied variable.
>
>It is used in a PowerPoint Module so will be VBA. I'll look over DateDiff to
>see if it returns the same results.

No real arithmetic required.

Dim t1 As Date
Dim t2 As Date
Dim d As Double
Const TXT = "Difference of Dates"

t1 = Now()
t2 = CDate(InputBox("Enter a date: ", TXT))
d = t1 - t2
MsgBox "Days: " & Format(Int(d), "#,###") _
& ", Hours: " & DatePart("h", d) _
& ", Minutes: " & DatePart("n", d), _
vbInformation + vbOKOnly, TXT

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Re: Cleaner Way to write this? by Dr

Dr
Wed Apr 12 11:23:38 CDT 2006

JRS: In article <OfydnfSVItbiaabZnZ2dnUVZ_umdnZ2d@comcast.com>, dated
Tue, 11 Apr 2006 14:20:21 remote, seen in news:microsoft.public.scriptin
g.vbscript, Yogi_Bear_79 <nospam@spamsux.com> posted :
>This works but I am looking for a cleaner way to write it. Also is there a
>way to display the seconds as well?


This (under-tested) runs for me in MSIE4 ...

F = #2006/04/28 11:00:00# ' FFF addicts won't get that so clean
T = F - Now
D = Int(T) : T = (T-D)*24
H = Int(T) : T = (T-H)*60
M = Int(T) : T = (T-M)*60
S = Int(T)
document.writeln _
"Days " & D & ", Hours " & H & ", Minutes " & M & ", " & S

Note that F is set by a CDate literal, for efficiency.

<URL:http://www.merlyn.demon.co.uk/vb-dates.htm> ff.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.