kunfuzed1 09-10-2003, 10:37 PM I'm getting a negative number using the datediff function..
If I go from 11:30pm to 12:01 am i get -1414 minutes instead of 31 minutes. I've tried using both the Time function and the Now function formatted to just showing the time.
How can I make it add correctly?
also, is it just me or is there something wrong with the search on this forum, off and on today i've been running into problems trying to search it..
Here is my code
Private Sub cmdCalculate_Click(Index As Integer)
If txtStartTime(Index).Text = "" Or txtEndTime(Index).Text = "" Then
MsgBox "You are missing either a start time or an end time", vbOKOnly, "Enter Times"
Else
dtStartTime(Index) = CDate(txtStartTime(Index).Text)
dtEndTime(Index) = CDate(txtEndTime(Index).Text)
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
txtTotalCost(Index).Text = txtTotalMinutes(Index).Text * txtRate(Index).Text
End If
End Sub
Legend 09-10-2003, 10:41 PM Check by printing the dates to the debug window. The CDate may not be working exactly as you think, especially as its coercing a date & time, not just a time. :)
kunfuzed1 09-10-2003, 11:13 PM Check by printing the dates to the debug window. The CDate may not be working exactly as you think, especially as its coercing a date & time, not just a time. :)
ok, debugging isnt a strong point of mine...i've been meaning to read up on some tutorials about that.. but i just try to make everything good so i have no bugs to debug :) i put debug.print in a few places and it caused run-time errors.
i took the cdate function out..
i've tried using both the Time function and the Now function to find the difference between the times and i still get negative numbers no matter which why i swing it.. apparently it's starting from 12:00 or somethin.. can you tell me how to do what im trying to do? i dont understand why its not doing it right.. i would think that if the dates switch, it would know the difference between 9-10-03 11:30pm and 9-12-03 12:05am
and i know there has to be a way to correct this, but i cant figure out what it is
Legend 09-10-2003, 11:21 PM I suggest you learn how to use the debug features - they are brilliant!
I have inserted a couple of debug lines below - you will need the immedaite window open to see the text they spit out:
Private Sub cmdCalculate_Click(Index As Integer)
If txtStartTime(Index).Text = "" Or txtEndTime(Index).Text = "" Then
MsgBox "You are missing either a start time or an end time", vbOKOnly, "Enter Times"
Else
dtStartTime(Index) = CDate(txtStartTime(Index).Text)
dtEndTime(Index) = CDate(txtEndTime(Index).Text)
debug.print "Start Time = " & dtStarttime(index)
debug.print "End Time = " & dtEndtime(index)
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
txtTotalCost(Index).Text = txtTotalMinutes(Index).Text * txtRate(Index).Text
End If
End Sub
jcheah 09-10-2003, 11:27 PM Your code worked fine for me...
declared variables as:
Dim dtStartTime, dtEndTime As Date
you get a big negative number cos it is comparing the times as if they were the same day....
i tried 16/01/1982 11:30pm and 17/01/1982 12:01am and got 31minutes..
it seems ok to me =)
kunfuzed1 09-10-2003, 11:58 PM Your code worked fine for me...
declared variables as:
Dim dtStartTime, dtEndTime As Date
you get a big negative number cos it is comparing the times as if they were the same day....
i tried 16/01/1982 11:30pm and 17/01/1982 12:01am and got 31minutes..
it seems ok to me =)
Yeah it works like that, but if i format the data, would that be whats changing it? I thought formatting it would only change how it appears in the text box, not the actual data it holds..
I'll post my total code...
Option Explicit
Dim dtStartTime(48) As Date ' Start Time Variables for moving players
Dim dtEndTime(48) As Date ' End Time Variables for moving players
Private Sub cmdCalculate_Click(Index As Integer)
If txtStartTime(Index).Text = "" Or txtEndTime(Index).Text = "" Then
MsgBox "You are missing either a start time or an end time", vbOKOnly, "Enter Times"
Else
dtStartTime(Index) = (txtStartTime(Index).Text)
dtEndTime(Index) = (txtEndTime(Index).Text)
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
txtTotalCost(Index).Text = txtTotalMinutes(Index).Text * txtRate(Index).Text
End If
End Sub
Private Sub cmdEndTime_Click(Index As Integer)
If txtStartTime(Index).Text = "" Then
MsgBox "You need to enter a start time first", vbOKOnly, "Enter A Start Time"
Else
txtEndTime(Index).Text = Format(Now, "hh:mm:ss AM/PM")
End If
End Sub
Private Sub cmdStartTime_Click(Index As Integer)
txtStartTime(Index).Text = Format(Now, "hh:mm:ss AM/PM")
End Sub
is the formatting killing me here?
I dont want the date to show in the text box, only the time.. i was using the Time function, but i was having the problem of it thinking it was the same day.. so thats why i am now using the Now Function but formatting it so that it shows only the hours/minutes/seconds..
is that what is screwing everything up ?
kunfuzed1 09-11-2003, 09:54 AM i just wanted to bump this back up to the top since i posted in the middle of the night and i see there are so many people online right now.. i'd really like to know why this isn't working right for me.
Thanks.
Agent707 09-11-2003, 10:33 AM declared variables as:
Dim dtStartTime, dtEndTime As Date
dtStartTime will be a variant data type.
Anytime you don't put "As Datatye" it makes it a variant.
you can't declare multiple items seperated with a comma like that as one data type... like you can with some other languages.
Just letting you know that.
Also, anytime you are getting a date from a textbox like this... you should use IsDate
Instead of
If txtStartTime(Index).Text = "" Then
Do This
If IsDate(txtStartTime(Index).Text) = False Then
'or
If Not IsDate(txtStartTime(Index).Text) Then
'No Date selected
That will catch "" and bad dates too.
Hope that helped
passel 09-11-2003, 10:55 AM Since you're just tracking the use of the table, and don't care about the
date, I would just add a check, and if the elapsed time is negative, add
1440 minutes (1 day) to it, which will give you the number you want.
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
if Val(txtTotalMinutes(Index).Text) < 0 then
txtTotalMinutes(Index).Text = Str$(Val(txtTotalMinutes(Index).Text + 1440))
end if
edit: Either that, or close the pool hall before midnight. :D
kunfuzed1 09-11-2003, 12:20 PM Since you're just tracking the use of the table, and don't care about the
date, I would just add a check, and if the elapsed time is negative, add
1440 minutes (1 day) to it, which will give you the number you want.
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
if Val(txtTotalMinutes(Index).Text) < 0 then
txtTotalMinutes(Index).Text = Str$(Val(txtTotalMinutes(Index).Text + 1440))
end if
edit: Either that, or close the pool hall before midnight. :D
heh, yeah, "guys, you gotta shut down before midnight because of my inept programming" heh that would go over well im sure :)
Thanks as always for the help, adding 1440 minutes does solve the problem. I just can't believe there isn't something already built into vb to handle this type of situation.. but hey, whatever works, im not picky as long as i get from A to B I don't care how i get there.
The only thing is now if it does add 1440, my txttotalminutes.text gets a space added to it before the number. it comes out " 60" instead of "60" .. totally minor issue, i could live with it.. but i cant see why its doing that.. just one of those little things that'll get to ya if u dont know why yanno? so if its obvious to someone why, feel free to share. if not. oh well , life will go on.
I appriciate everyone for replying and giving me help.
agent 707, the isdate function is something i wasnt aware of, thanks. I'll be using that.
passel my man, as always, thanks
passel 09-11-2003, 02:11 PM The space is added by the Str$ function. It adds a space for alignment
purposes (if the value was negative then a "-" goes where the space is).
Of course, since we rarely use fixed spaced fonts any more, the idea of
alignment is kind of moot, since the space will probably not be the same
width as a "-".
Since an asignment to the textbox will automatically convert a number
to a string without the leading space, you can remove the Str$ function. In
fact, since VB does all these conversions for you, you could simplify it to
txtTotalMinutes(Index).Text = txtTotalMinutes(Index).Text + 1440
and get the result that you want. I had the parens. in the wrong place
in anycase for the Val function, should have been
txtTotalMinutes(Index).Text = Str$(Val(txtTotalMinutes(Index).Text) + 1440)
But the Str$ function would still give you the leading space. It's just
making my living with "Ada" for the last 12 years, tends to make me
use explicit type conversions, otherwise it feels like something is "missing" to me.
Agent707 09-11-2003, 02:44 PM One thing I meant to add before... If you include the date it will resolve your problem too.
DateDiff("n","09/10/2003 11:01 PM","09/11/2003 12:45 AM")
Will return 104
cheers
Sorry, I kinda jumped around the thread. No, formatting does not mess the date up.
CDate("01-01-03 12:00 PM") results in 01/01/2003 12:00PM
...which works fine.
you DO have to have the date in for it to work, unless you add the 1440 minutes as he suggested.
passel 09-11-2003, 03:31 PM I just can't believe there isn't something already built into vb to handle this type of situation..
Oh Yes, I was going to comment on that too. As Agent707 has pointed
out, the proper way would be to specifiy the date, but I know since this
is for tracking how long a pool table has been rented for, you wouldn't
want the inconvience of having to continually specify the date, or take
up real-estate on the form to add it.
Since the DateDiff will give you the difference in two times either forward, or backward, it can't assume which perspective you are
asking for (future or past). If you don't specify the date, then both
times are taken as being on the same day, and 12:01 am on any given
day occurs 23 hours and 29 minutes before 11:30 pm on the same day,
which is why you get -1409 minutes. Since you know the 12:01 is
actually on the next day, if you don't tell Visual Basic it's on the next
day, then you have to adjust for it yourself, as we did.
kunfuzed1 09-13-2003, 03:35 PM Hey guys,
I think I finally figured out where the problem has been with all of this..
I think the formatting was screwing it up all along, because in my code, I was pulling the numbers from the textbox, and since it was formatted for medium time, thats why it wasnt keeping track of the difference between the dates, only the times, because that's what was in the .text property of my text boxes..
I've been struggling with this for awhile and you guys too by helping me out, so i thought I'd share what I'm pretty sure the problem has been all this time. I'm still not sure off the top of my head how I'm going to fix it, but at least I think I know where to start now. I could add 1440 as you said passel, but now that I've thought about it, I do want to keep track of the date because eventually I need to figure out how to put all this stuff into a database or something to keep track of daily/weekly/monthly numbers.
So my task is now trying to figure out how I'm going to Get the Date/Time, but only show the Time in the text box, yet keep the date somewhere in the background so that I can do the math and have all the numbers come out right.
passel 09-13-2003, 05:36 PM I would just store the time with date in variables, which would also be
what I wrote to the disk. But for display, I would display the variable
without the date. Example:
Dim StartTime(54) As Date
Dim EndTime(54) As Date
Private Sub cmdStartTime_Click(Index As Integer)
StartTime(Index) = CDate(Format$(Now, "yyyy/mm/dd h:mm"))
txtStartTime(Index).Text = Format$(StartTime(Index), "h:mm am/pm")
End Sub
Private Sub cmdEndTime_Click(Index As Integer)
EndTime(Index) = CDate(Format$(Now, "yyyy/mm/dd h:mm"))
txtEndTime(Index).Text = Format$(EndTime(Index), "h:mm am/pm")
End Sub
Private Sub cmdCalculate_Click(Index As Integer)
Dim TotalMinutes As Long
TotalMinutes = DateDiff("n", StartTime(Index), EndTime(Index))
txtTotalMinutes(Index) = TotalMinutes
txtTotalCost(Index) = Format$(TotalMinutes * Val(txtRate(Index).Text), "0.00")
End Sub
kunfuzed1 09-17-2003, 01:17 PM Thanks passel, your help has been much appriciated through all of this.
I wish I would've found this forum years ago, I may not have taken such a long break from my learning. You guys are all awesome.
Thanks!
00100b 09-17-2003, 01:45 PM What about just doing a simple check?
Private Sub cmdCalculate_Click(Index As Integer)
If txtStartTime(Index).Text = "" Or txtEndTime(Index).Text = "" Then
MsgBox "You are missing either a start time or an end time", vbOKOnly, "Enter Times"
Else
dtStartTime(Index) = CDate(txtStartTime(Index).Text)
dtEndTime(Index) = CDate(txtEndTime(Index).Text)
If dtEndTime(Index) < dtStartTime(Index) Then
dtEndTime(Index) = DateAdd("d", 1, dtEndTime(Index))
End If
txtTotalMinutes(Index).Text = DateDiff("n", dtStartTime(Index), dtEndTime(Index))
txtTotalCost(Index).Text = txtTotalMinutes(Index).Text * txtRate(Index).Text
End If
End Sub
|