[RESOLVED] working with 24 hour times
I have some 24 hour times that i am trying to compare to the users pc time
task time = 0500 thats 5:AM
say the users pc time is 3:AM
The Difference is 2 hours
task time = 1700 thats 5:PM
say the users pc time is 4:PM
The Difference is 1 hour
how can this be calculated with vb6 ?
Re: working with 24 hour times
DateDiff can accept 24 or 12 hour times, i.e.,
MsgBox DateDiff("n","17:00:00", "6:00:00 PM")
Re: working with 24 hour times
Quote:
Originally Posted by
LaVolpe
DateDiff can accept 24 or 12 hour times, i.e.,
MsgBox DateDiff("n","17:00:00", "6:00:00 PM")
thanks
my problem is i do have a date the 1700 is part of a file name
Dy1700.rtf with the Dy and .rtf removed i am trying compare the current time to the 1700
this outputs 2490120
Dim mytime
mytime = 1730
MsgBox DateDiff("n", mytime, "6:00:00 PM")
Re: working with 24 hour times
DateDiff requires the final two parameters to be a date and/or time format. This means you will have to parse the 1700 and determine where to place the colon, i.e., 17:00
Re: working with 24 hour times
Quote:
Originally Posted by
LaVolpe
DateDiff requires the final two parameters to be a date and/or time format. This means you will have to parse the 1700 and determine where to place the colon, i.e., 17:00
Actually it requires two Date type values. You can ram Strings in there and rely on coercion to muddle through but that is always a crapshoot due to localization and punctuation issues.
It is probably better to just write your own functions to parse String data into Date typed data for programs that need to do this. Otherwise you are just Trusting The Force and letting the underlying OLE Automation functions roll the dice, which means you have to accept that it is going to make wrong (sometimes very wrong) guesses. This leads directly to data corruption that can have cumulative destructive effects over time.
Re: working with 24 hour times
Thanks for your help
just trying to make a little app to remind my sister when to take her medicine with a messagebox
Re: working with 24 hour times
Quote:
Originally Posted by
isnoend07
thanks
my problem is i do have a date the 1700 is part of a file name
Dy1700.rtf with the Dy and .rtf removed i am trying compare the current time to the 1700
this outputs 2490120
Dim mytime
mytime = 1730
MsgBox DateDiff("n", mytime, "6:00:00 PM")
In this case your myTime variable is a variant that holds a number and that number is then trying to be interpreted as a date. Of course that resulting date is not even close to what you were trying to feed it.
You could use a string "17:00" or better use a date var with the proper value stuffed in it.
You should always use the As portion for every variable you use and you should always try and make sure that the variable type you decide on is the correct one for what you are doing.
Try this
Code:
Dim mytime As Date
mytime = CDate("17:30")
MsgBox DateDiff("n", mytime, "6:00:00 PM")
Or better this
Code:
Dim mytime As Date
mytime = CDate("17:30")
MsgBox DateDiff("n", mytime, CDate("6:00:00 PM"))
Re: [RESOLVED] working with 24 hour times
To give you a better idea of why your first attempt did not work try this and see what it gives you
Code:
Dim mytime
mytime = 1730
MsgBox CDate(mytime)
You'll noticed that it is a far cry from what you thought you were giving it.
Re: working with 24 hour times
Quote:
Originally Posted by
DataMiser
In this case your myTime variable is a variant that holds a number and that number is then trying to be interpreted as a date. Of course that resulting date is not even close to what you were trying to feed it.
You could use a string "17:00" or better use a date var with the proper value stuffed in it.
You should always use the As portion for every variable you use and you should always try and make sure that the variable type you decide on is the correct one for what you are doing.
Try this
Code:
Dim mytime As Date
mytime = CDate("17:30")
MsgBox DateDiff("n", mytime, "6:00:00 PM")
Or better this
Code:
Dim mytime As Date
mytime = CDate("17:30")
MsgBox DateDiff("n", mytime, CDate("6:00:00 PM"))
I tested the code and it produces 30
what does 30 mean ?
Re: [RESOLVED] working with 24 hour times
Quote:
Originally Posted by
DataMiser
To give you a better idea of why your first attempt did not work try this and see what it gives you
Code:
Dim mytime
mytime = 1730
MsgBox CDate(mytime)
You'll noticed that it is a far cry from what you thought you were giving it.
that produces 9/25/1904
Re: working with 24 hour times
Quote:
Originally Posted by
isnoend07
I tested the code and it produces 30
what does 30 mean ?
30 minutes of course. the "n" is telling it to give your the difference in minutes
and the other one shows you why you got such a large number because rather than comparing two times that were 30 minutes apart you were using times that were over 100 years apart