Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateAdd problem

Status
Not open for further replies.

core2max

Technical User
Joined
Jul 23, 2004
Messages
3
Location
SE
What am I doing wrong?
(WinXP, VB-engine:5.6)

CODE:
Option Explicit

Dim tTime, tTest1, tTest2
tTime = CDate(Date & " 22:45:00")
tTest1 = CDate(Date & " 23:00:00")
tTest2 = CDate(Date & " 23:15:00")

tTime = DateAdd("n",15,tTime) 'add 15min

if tTime = tTest1 then
msgbox "tTime is EQUAL to tTest1"
else
msgbox "According to MS tTime (" & tTime & ") is NOT EQUAL TO tTest1(" & tTest1 & ")"
end if

tTime = DateAdd("n",15,tTime) 'add 15min

if tTime = tTest2 then
msgbox "BUT this is... tTime (" & tTime & ") is NOT EQUAL TO tTest2(" & tTest2 & ")"
end if
 
DateTime variables are in fact numbers stored in double precision real that are not well suited for EXACT match.
As a workaround you may consider this:
If FormatDateTime(tTime) = FormatDateTime(tTest1) Then


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hello core2max,

Just a note further to PHV's. Your last message is a misunderstanding. tTime is added twice 15mins, hence it should be "EQUAL TO" tTest2, and in fact it does even without formatdatetime.

regards - tsuji
 
Thanks :)

I've now solved my problem the hard way...
Using FormatDateTime to check the time and the date sepratly.

/Core2max!
 
Using FormatDateTime to check the time and the date sepratly
Why separatly ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm creating a table for filling out working hours.
Using style and javascript on the client side.

Monday
------------------------
00:00-01:00 |
------------------------
01:00-02:00 |
------------------------
...
..
.
------------------------
23:00-00:00 |
------------------------


Each hour is divided into four segments of 15min.
The user can create multiple periods for each day - each period is color coded using javascript and style.
When I save the periods to the DB I save them like this:

iDayID | tStart | tStop |
-------|-------------------|-------------------|
1 1900-01-01 00:15:00 1900-01-01 02:15:00
1 1900-01-01 17:00:00 1900-01-01 21:00:00
3 1900-01-01 00:15:00 1900-01-01 02:15:00
and so on...

When recreating the table of working hours I need to loop through each segment to give it the right color (24h*4seg = 96segment for each day). That is where I use DateAdd to add 15m after each loop.
When the time is 23:45 and you add 15m this will result in '1900-01-02 00:00:00' - a new day. That's why I have to check the date.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top