473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

More trouble getting Dates

What is it with these dates? What are they so much trouble? All I want to
do is subtract an hour, but not if that makes dtmTest less than dtmClt.

Dim dtmStart As Date
Dim dtmEnd As Date
Dim dtmCtl As Date
Dim dtmTest As Date

Me!cbxEndTime.Requery
Me!cbxStartTime.Requery

dtmStart = #1/1/1000# & " " & Me!cbxStartTime
dtmEnd = #1/1/1000# & " " & Me!cbxEndTime
dtmCtl = #1/1/1000# & " " & "12:00 AM"
dtmTest = DateAdd("h", -1, dtmEnd)

Debug.Print Format((dtmStart), "m/d/yyyy h:nn am/pm") & " = start"
Debug.Print Format((dtmEnd), "m/d/yyyy h:nn am/pm") & " = end"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm") & " = control"
Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm") & " = test"
Debug.Print

If t = "start" Then

If dtmTest < dtmCtl Then

Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm")
Debug.Print "is less than"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm")

FixTime = Format(TimeValue(dtmEnd), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", -1, dtmEnd)), "h:nn am/pm")
End If

End If

[output]

1/1/1000 4:30 am = start
1/1/1000 4:00 am = end
1/1/1000 12:00 am = control
1/1/1000 3:00 am = test

1/1/1000 3:00 am
is less than
1/1/1000 12:00 am

But dtmTest is NOT less than dtmCtl.... why all the drama? Seems too high
maintenance for me.
Nov 13 '05 #1
9 2025
On Tue, 28 Dec 2004 02:17:52 GMT, "deko"
<www.clearpointsystems.com@use_contact_form.com> wrote:

To compare dates, use the DateDiff function.
-Tom.

What is it with these dates? What are they so much trouble? All I want to
do is subtract an hour, but not if that makes dtmTest less than dtmClt.

Dim dtmStart As Date
Dim dtmEnd As Date
Dim dtmCtl As Date
Dim dtmTest As Date

Me!cbxEndTime.Requery
Me!cbxStartTime.Requery

dtmStart = #1/1/1000# & " " & Me!cbxStartTime
dtmEnd = #1/1/1000# & " " & Me!cbxEndTime
dtmCtl = #1/1/1000# & " " & "12:00 AM"
dtmTest = DateAdd("h", -1, dtmEnd)

Debug.Print Format((dtmStart), "m/d/yyyy h:nn am/pm") & " = start"
Debug.Print Format((dtmEnd), "m/d/yyyy h:nn am/pm") & " = end"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm") & " = control"
Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm") & " = test"
Debug.Print

If t = "start" Then

If dtmTest < dtmCtl Then

Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm")
Debug.Print "is less than"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm")

FixTime = Format(TimeValue(dtmEnd), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", -1, dtmEnd)), "h:nn am/pm")
End If

End If

[output]

1/1/1000 4:30 am = start
1/1/1000 4:00 am = end
1/1/1000 12:00 am = control
1/1/1000 3:00 am = test

1/1/1000 3:00 am
is less than
1/1/1000 12:00 am

But dtmTest is NOT less than dtmCtl.... why all the drama? Seems too high
maintenance for me.


Nov 13 '05 #2
Here's output that shows how the Function is supposed to work.

1/1/1000 12:30 am = start
1/1/1000 12:00 am = end
1/1/1000 12:00 am = control
12/31/0999 11:00 pm = test

12/31/0999 11:00 pm
is less than
1/1/1000 12:00 am

The Bible says we should not be unequally yoked, so I tried converting my
Date like this:

dtmStart = CDate(#1/1/1000# & " " & Me!cbxStartTime)
dtmEnd = CDate(#1/1/1000# & " " & Me!cbxEndTime)
dtmCtl = CDate(#1/1/1000# & " " & "12:00 AM")

All to no avail. So I'm moving on to a new, younger Date:

dtmStart = CDate(DateValue(Date) & " " & Me!cbxStartTime)
dtmEnd = CDate(DateValue(Date) & " " & Me!cbxEndTime)
dtmCtl = CDate(DateValue(Date) & " " & "12:00 AM")

Now that's a thing of beauty!
Nov 13 '05 #3
Well, you may not get what you think you should when you compare the
character strings that result from a Format statement, as you were doing
earlier. It's no wonder that date comparisons work better using date/time
variables than they do using strings.

Larry Linson
Microsoft Access MVP

"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:aZ*****************@newssvr14.news.prodigy.co m...
Here's output that shows how the Function is supposed to work.

1/1/1000 12:30 am = start
1/1/1000 12:00 am = end
1/1/1000 12:00 am = control
12/31/0999 11:00 pm = test

12/31/0999 11:00 pm
is less than
1/1/1000 12:00 am

The Bible says we should not be unequally yoked, so I tried converting my
Date like this:

dtmStart = CDate(#1/1/1000# & " " & Me!cbxStartTime)
dtmEnd = CDate(#1/1/1000# & " " & Me!cbxEndTime)
dtmCtl = CDate(#1/1/1000# & " " & "12:00 AM")

All to no avail. So I'm moving on to a new, younger Date:

dtmStart = CDate(DateValue(Date) & " " & Me!cbxStartTime)
dtmEnd = CDate(DateValue(Date) & " " & Me!cbxEndTime)
dtmCtl = CDate(DateValue(Date) & " " & "12:00 AM")

Now that's a thing of beauty!

Nov 13 '05 #4
> Well, you may not get what you think you should when you compare the
character strings that result from a Format statement, as you were doing
earlier. It's no wonder that date comparisons work better using date/time
variables than they do using strings.


The problem now is the date fails to roll over to the next day. My guess is
that's because the variable is built on the Date() function. Is this a
situation where I should pass in the date variables by Ref?

Dim dtmStart As Date
Dim dtmEnd As Date
Dim dtmCtl As Date
Dim dtmTest As Date

Me!cbxEndTime.Requery
Me!cbxStartTime.Requery
dtmStart = DateValue(Date) & " " & Me!cbxStartTime
dtmEnd = DateValue(Date) & " " & Me!cbxEndTime

If ...

[code omitted]

ElseIf t = "end" Then
dtmTest = DateAdd("h", 1, dtmEnd)
dtmCtl = DateValue(Date) & " " & "11:30 PM"

Debug.Print Format((dtmStart), "m/d/yyyy h:nn am/pm") & " = start"
Debug.Print Format((dtmEnd), "m/d/yyyy h:nn am/pm") & " = end"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm") & " = control"
Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm") & " = test"

If dtmTest > dtmCtl Then
FixTime = Format(TimeValue(dtmStart), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", 1, dtmStart)), "h:nn am/pm")
End If

[output]

12/27/2004 11:30 pm = start
12/27/2004 12:30 am = end
12/27/2004 11:30 pm = control
12/27/2004 1:30 am = test

What happened is that an hour was added to the start date to get "12/27/2004
1:30 am" as a value for FixTime. The problem is that this should have met
the "dtmTest > dtmCtl" condition (i.e. 12/28/2004 1:30 am > 12/27/2004 11:30
pm) and returned the start date. Should I make another function and pass
the start and end variables in by Ref? I think VBA passes ByVal by
default - is this correct? Perhaps there is a better way around this?

Thanks for the help!
Nov 13 '05 #5
typo:
What happened is that an hour was added to the start date to get "12/27/2004 1:30 am" as a value for FixTime. The problem is that this should have met


correction:

.... to the start date to get *12/27/2004 12:30 am* as a value for FixTime.
Nov 13 '05 #6
You are relying on Access' implicit coercion of data types, and I don't know
how many are required in the expressions you show...
try using the CDat function on the start and end times, and adding the Date
and Time.

I am not sure what benefit you would get by using DateValue on the
Variant(Date) returned by the Date function. The "date" that is an argument
to DateValue is normally a String.

And, frankly, if it doesn't matter whether the date part is 1/1/1000 or the
current date, I am puzzled as to what it is you are trying to do...

Reading Help on these functions and date manipulations would be a good start
here.

Here are a couple of things I tried in the Immediate Window:

? Date() + Time()
12/28/2004 6:49:58 PM
? Date() + CDate("06:49:58 PM")
12/28/2004 6:49:58 PM

I hope someone will pick up this thread because I am not going to be
"newsgrouping" for several days, starting tomorrow.

Larry Linson
Microsoft Access MVP
"deko" <www.clearpointsystems.com@use_contact_form.com> wrote in message
news:6_****************@newssvr13.news.prodigy.com ...
Well, you may not get what you think you should when you compare the
character strings that result from a Format statement, as you were doing
earlier. It's no wonder that date comparisons work better using date/time variables than they do using strings.
The problem now is the date fails to roll over to the next day. My guess

is that's because the variable is built on the Date() function. Is this a
situation where I should pass in the date variables by Ref?

Dim dtmStart As Date
Dim dtmEnd As Date
Dim dtmCtl As Date
Dim dtmTest As Date

Me!cbxEndTime.Requery
Me!cbxStartTime.Requery
dtmStart = DateValue(Date) & " " & Me!cbxStartTime
dtmEnd = DateValue(Date) & " " & Me!cbxEndTime

If ...

[code omitted]

ElseIf t = "end" Then
dtmTest = DateAdd("h", 1, dtmEnd)
dtmCtl = DateValue(Date) & " " & "11:30 PM"

Debug.Print Format((dtmStart), "m/d/yyyy h:nn am/pm") & " = start"
Debug.Print Format((dtmEnd), "m/d/yyyy h:nn am/pm") & " = end"
Debug.Print Format((dtmCtl), "m/d/yyyy h:nn am/pm") & " = control"
Debug.Print Format((dtmTest), "m/d/yyyy h:nn am/pm") & " = test"

If dtmTest > dtmCtl Then
FixTime = Format(TimeValue(dtmStart), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", 1, dtmStart)), "h:nn am/pm") End If

[output]

12/27/2004 11:30 pm = start
12/27/2004 12:30 am = end
12/27/2004 11:30 pm = control
12/27/2004 1:30 am = test

What happened is that an hour was added to the start date to get "12/27/2004 1:30 am" as a value for FixTime. The problem is that this should have met
the "dtmTest > dtmCtl" condition (i.e. 12/28/2004 1:30 am > 12/27/2004 11:30 pm) and returned the start date. Should I make another function and pass
the start and end variables in by Ref? I think VBA passes ByVal by
default - is this correct? Perhaps there is a better way around this?

Thanks for the help!

Nov 13 '05 #7
> Here are a couple of things I tried in the Immediate Window:

? Date() + Time()
12/28/2004 6:49:58 PM
? Date() + CDate("06:49:58 PM")
12/28/2004 6:49:58 PM


Thanks for the reply. I'm getting along better with my date now :)

What I'm trying to do is automatically adjust time values in two combo
boxes - a start time and an end time.

If the user selects a start time that occurs after the end time, (or and end
time that occurs before the start time) the end time (or start time) is
automatically fixed by adding (or subtracting) an hour to the selected time.
But if the adjustment would cause the "fixed" time to exceed the current
day, then just "fix" it to the same value as the selected time. The times
must stay within the current day or I get a negative Duration (which is
calculated from the values in the combo boxes) that causes problems
elsewhere.

My problem was that I thought I needed a full date value for a control value
that would circumscribe a 24-hr day. I've discovered that time values work
just fine. So, I have two time values - 12:00 AM and 11:59 PM - and compare
the proposed "fixed" times to the control value and adjust from there.

Private Function FixTime(t As String) As String
Dim dtmStart As Date
Dim dtmEnd As Date
Dim dtmCtl As Date
Dim dtmTest As Date
Me!cbxEndTime.Requery
Me!cbxStartTime.Requery
dtmStart = TimeValue(Me!cbxStartTime)
dtmEnd = TimeValue(Me!cbxEndTime)
If t = "start" Then
dtmTest = DateAdd("h", -1, dtmEnd)
dtmCtl = TimeValue("12:00 AM")
If dtmTest < dtmCtl Then
FixTime = Format(TimeValue(dtmEnd), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", -1, dtmEnd)), "h:nn
am/pm")
End If
ElseIf t = "end" Then
dtmTest = DateAdd("h", 1, dtmStart)
dtmCtl = TimeValue("11:59 PM")
If dtmTest > dtmCtl Then
FixTime = Format(TimeValue(dtmStart), "h:nn am/pm")
Else
FixTime = Format(TimeValue(DateAdd("h", 1, dtmStart)), "h:nn
am/pm")
End If
End If
End Function

I'm not sure if I'll stick with this forever, but it's okay for now :)
Nov 13 '05 #8
One thing that might help is knowing that the date is the integer
portion, while the time is the decimal portion. So to add a day, you
can simply add 1 to your datetime value. Adding 1/24 (0.0416666666
yadah,yadah) would add one hour, adding 1/(24*60)
(0.000694444444444) would add one minute.

ALWAYS do date/time calculations in date/time variables - it just
doesn't pay to try to deal with them otherwise. Working in date/time
and convert it at the end if you need to display it, but all
calculations and comparisions should be in date/time.

Nov 13 '05 #9
> One thing that might help is knowing that the date is the integer
portion, while the time is the decimal portion. So to add a day, you
can simply add 1 to your datetime value. Adding 1/24 (0.0416666666
yadah,yadah) would add one hour, adding 1/(24*60)
(0.000694444444444) would add one minute.
helpful tip.
ALWAYS do date/time calculations in date/time variables - it just
doesn't pay to try to deal with them otherwise. Working in date/time
and convert it at the end if you need to display it, but all
calculations and comparisions should be in date/time.


10-4
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
8
by: Eric A. Johnson | last post by:
I am using scanf (from stdio.h) and having trouble inputting a date. I want the user to be able to input a date in the format mm/dd/yyyy. I use the following: printf ("Please enter your first...
1
by: Giordano | last post by:
I am trying to construct a report menu that will allow the user to select any combination of 3 variables from 3 combo boxes (Select Subject/SelectCategory/Date Range). There are 2 possible options...
4
by: Stan | last post by:
I am running in ACCESS 2003 a database with a single table. It records service rendered to clients of a food pantry. As each client is served the date is entered into 1-6 fields SvcDate1,...
13
numberwhun
by: numberwhun | last post by:
Hello everyone! I have a data file that contains miscellaneous information on each line. (Unfortunately, I cannot go into detail of the file layout as it is sensitive information), but I can say...
3
by: lostdawg | last post by:
Hi, I am having trouble with the following query. I need to sort from a list of contacts the last date each was contacted. This is to be represented in days so for instance: 0-42 days...
8
by: Innocent2104 | last post by:
Hi there, The script below displays the attached output but as shown, it skips certain days and i need to include these to calculate my avg balance for a certain month, i.e.Nov. How do i update the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.