Small VBS to demonstrate an issue I have with the VBS DateAdd function:

msgbox CDate("23/10/2005 22:15:00") = DateAdd("h", 22, DateAdd("n", 15,
CDate("23/10/2005")))

This pops up a message box with False in it.

Any suggestion as to why?

This returns true:

msgbox CDate("23/10/2005 22:15:00") = CDate(CStr(DateAdd("h", 22,
DateAdd("n", 15, CDate("23/10/2005")))

(UK Locale!)

Re: DateAdd Bug? by ekkehard

ekkehard
Wed Nov 02 08:55:08 CST 2005

Michael Jervis wrote:
> Small VBS to demonstrate an issue I have with the VBS DateAdd function:
>
> msgbox CDate("23/10/2005 22:15:00") = DateAdd("h", 22, DateAdd("n", 15,
> CDate("23/10/2005")))
>
> This pops up a message box with False in it.
>
> Any suggestion as to why?
>
> This returns true:
>
> msgbox CDate("23/10/2005 22:15:00") = CDate(CStr(DateAdd("h", 22,
> DateAdd("n", 15, CDate("23/10/2005")))
>
> (UK Locale!)
>
Comparing dates (== doubles) with "=" is dangerous (limited
number of bits to represent an unlimited range of values).
From this code:

Dim dtFrs : dtFrs = CDate( "23/10/2005 22:15:00" )
Dim dtSec : dtSec = DateAdd( "h", 22, DateAdd( "n", 15, CDate( "23/10/2005" ) ) )
Dim dtThd : dtThd = CDate( "23/10/2005 22:15:00" )
WScript.Echo "1", dtFrs , dtSec
WScript.Echo "2", CDbl( dtFrs ) , CDbl( dtSec )
WScript.Echo "3", dtFrs = dtSec
WScript.Echo "4", CDate( dtFrs ) = CDate( dtSec )
WScript.Echo "5", CStr( dtFrs ) = CStr( dtSec )
WScript.Echo "6", dtFrs = dtThd
------
1 23.10.2005 22:15:00 23.10.2005 22:15:00
2 38648,9270833333 38648,9270833333
3 0
4 0
5 -1
6 -1

I assume that VBScript treats Dates as Doubles when comparing
them.

Re: DateAdd Bug? by Ray

Ray
Wed Nov 02 09:05:00 CST 2005

Yeah, I'm getting the same results as you, and I tried a number of variants
all returning inaccurate results. Hopefully someone can explain it. If
nothing else, if you just want a work around, I believe using DateValue()
will cure this.

msgbox DateValue(CDate("23/10/2005 22:15:00")) = DateValue(DateAdd("h", 22,
DateAdd("n", 15, CDate("23/10/2005"))))


Ray at work



"Michael Jervis" <mjervis@gmail.com> wrote in message
news:1130934124.816534.254240@g43g2000cwa.googlegroups.com...
> Small VBS to demonstrate an issue I have with the VBS DateAdd function:
>
> msgbox CDate("23/10/2005 22:15:00") = DateAdd("h", 22, DateAdd("n", 15,
> CDate("23/10/2005")))
>
> This pops up a message box with False in it.
>
> Any suggestion as to why?
>
> This returns true:
>
> msgbox CDate("23/10/2005 22:15:00") = CDate(CStr(DateAdd("h", 22,
> DateAdd("n", 15, CDate("23/10/2005")))
>
> (UK Locale!)
>



Re: DateAdd Bug? by Dr

Dr
Thu Nov 03 10:56:17 CST 2005

JRS: In article <1130934124.816534.254240@g43g2000cwa.googlegroups.com>
, dated Wed, 2 Nov 2005 04:22:04, seen in news:microsoft.public.scriptin
g.vbscript, Michael Jervis <mjervis@gmail.com> posted :
>Small VBS to demonstrate an issue I have with the VBS DateAdd function:
>
>msgbox CDate("23/10/2005 22:15:00") = DateAdd("h", 22, DateAdd("n", 15,
>CDate("23/10/2005")))
>
>This pops up a message box with False in it.
>
>Any suggestion as to why?
>
>This returns true:
>
>msgbox CDate("23/10/2005 22:15:00") = CDate(CStr(DateAdd("h", 22,
>DateAdd("n", 15, CDate("23/10/2005")))
>
>(UK Locale!)

Dates are stored, in IEEE Doubles IIRC, in units of Days since 0 =
Saturday 1899-12-30 00:00 local.

Therefore, neither 22 hours, nor 15 minutes, nor 22:15:00, can be stored
exactly. MsgBox the difference, rather than the equality, and you may
see non-zero.

If you want accuracy in minutes, multiply by 1440 and use exact minutes
or round to the nearest.

CDate("03/11/2005") is unsafe, since there's a risk of going American
and getting Feb 11 - CDate("03 Nov 2005") is safe everywhere.


To add 22h 15m to a date D, I'd use D + 22/14 + 15/1440 or D + 22.25/24.


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

--
© 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.