Find out a time between two times
Hi
I want to find out a time between two times.
For an example my program must be closed automatically at a time between 1 am and 6 am
I use a timer to check the time
My code is
Code:
Private Sub Timer1_Timer()
If Time > "1:00:00 AM" And Time < "6:00:00 AM" Then
End
End If
End Sub
But this is always true and close my program at any time.
Can anybody suggest a solution to close my program between 1 am and 6 am
Nasreen
Re: Find out a time between two times
Time returns a Date datatype. It is dangerous to do comparisons to strings. Thus: use a TimeSerial function to create a time of the Date datatype.
Code:
If Time >= TimeSerial(1, 0, 0) And Time < TimeSerial(6, 0, 0) Then
Unload Me
End If
End is also dangerous; see the FAQ forum for more information.
Re: Find out a time between two times
ahlan nasreen.
time value is string. and you cannt use "<,>...." to compare. strings.
look at merri code. it's works.
aslamo aliykom
Re: Find out a time between two times
my suggestion...
use datediff() read on more about it
Example
Code:
'To find Hours
DateTime.DateDiff("h",Text1.Text,now)
'To find Minutes
DateTime.DateDiff("n",Text1.Text,now)
'To find Seconds
DateTime.DateDiff("s",Text1.Text,now)
Re: Find out a time between two times
koolsid: the problem is that you get the total time for each, for 8 hours you get 8 hours, 480 minutes and 28800 seconds.
Re: Find out a time between two times
Quote:
Originally Posted by yosef_mreh
ahlan nasreen.
time value is string. and you cannt use "<,>...." to compare. strings.
look at merri code. it's works.
aslamo aliykom
That's a broad statement. While math operators like '+' should not be used to concatenate strings I've been using '< >' for basic string comparison forever with no ill effects. Actually MSDN doesn't list '< >' as exclusively a math comparator. Neither is '='. How would you write this?
Code:
Option Explicit
Private Sub Form_Load()
Dim strOne As String
Dim strTwo As String
Dim strResult As String
strOne = "Hello"
strTwo = "hello"
If strTwo <> strOne Then
MsgBox "strTwo does not equal strOne"
End If
If strTwo = strOne Then
MsgBox "strOne equals strTwo"
End If
End Sub
Re: Find out a time between two times
Quote:
Originally Posted by CDRIVE
That's a broad statement. While math operators like '+' should not be used to concatenate strings I've been using '< >' for basic string comparison forever with no ill effects. Actually MSDN doesn't list '< >' as exclusively a math comparator. Neither is '='. How would you write this?
Code:
Option Explicit
Private Sub Form_Load()
Dim strOne As String
Dim strTwo As String
Dim strResult As String
strOne = "Hello"
strTwo = "hello"
If strTwo <> strOne Then
MsgBox "strTwo does not equal strOne"
End If
If strTwo = strOne Then
MsgBox "strOne equals strTwo"
End If
End Sub
using equal or not equal. not like to use larg than">" ot else.
math function is som'n else.
Re: Find out a time between two times
u can use this code and it's work.
if time > format("1:00:00 ","hh") or time < format("6:00:00"."hh") then
end
end if
Re: Find out a time between two times
You shouldn't be using Strings for time values, as it can create many problems - instead you should be using Dates. For more information, see the article Why are my dates not working properly? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)
Another FAQ article you should take a look at is Why is using the 'End' statement (or VB's "stop" button) a bad idea?
Quote:
Originally Posted by yosef_mreh
using equal or not equal. not like to use larg than">" ot else.
math function is som'n else.
You can use < or > for Strings, but you need to be aware that the comparison is character based (from the left) rather than based on numeric (from the right/decimal point) or date (from various positions), etc.
Re: Find out a time between two times
Date comparison is a numeric comparison: internally Date datatype is just a regular Double. VB just adds in some intelligent handling to figure out dates based on the numeric values.
When using times in dates, we are dealing with values in the range 0 <= TIME < 1 — this means 0 = 00:00, 0.5 = 12:00, 0.25 = 06:00 and 0.75 = 18:00 (using the 24 hour clock because it is superior).
Re: Find out a time between two times
Quote:
Originally Posted by yosef_mreh
using equal or not equal. not like to use larg than">" ot else.
math function is som'n else.
In the OP's case time is a date comparison so should be used.
But just to clarify, you can use greater than (>) or less than (<) for strings. An example would be to use as part of a bubble sort of a list of words.
In the OP's case, I'm not advocating he do it this way, but technically it would work. Get rid of the AM/PM and use a 24 hour clock:
Code:
dim TestTime as string
TestTime=format(Time,"HH:mm:ss")
If TestTime >= "01:00:00" And TestTime < "06:00:00" Then
By far in this particular case, since the times are 1:00 am and 6:00 am respectively, the easiest way would simply be like this:
Code:
If Hour(Time)>= 1 and Hour(Time) < 6 then
This is strictly a numeric comparison and doesn't mess with dates directly, or having to format anything
Re: Find out a time between two times
Quote:
Originally Posted by yosef_mreh
time value is string.
Time value is not String, it is Date data type. You can check that with
Debug.Print TypeName(Time)
However with expression Time > "1:00:00 AM",
because the RHS is a String so the LHS will be converted to a String before comparison occurs.
Eg., with Time = #10:23:15 AM#, the above expression will become
"10:23:15 AM" > "1:00:00 AM".
Because the 1st characters "1" = "1" but the 2nd characters "0" < ":", so
"10:23:15 AM" > "1:00:00 AM" is FALSE and
"10:23:15 AM" < "6:00:00 AM" is TRUE (because "1" < "6")
Instead of: Time > "1:00:00 AM",
you can use: Time > TimeSerial(1, 0, 0) as Merri suggested
or you can also use: Time > TimeValue("1:00:00 AM"),
or perhaps easier: Time > #1:00:00 AM#,
Your code can be written as:
Code:
Private Sub Timer1_Timer()
If Time > #1:00:00 AM# And Time < #6:00:00 AM# Then
Unload Me
End If
End Sub
Noted that there are 2 Time values in the line
If Time > #1:00:00 AM# And Time < #6:00:00 AM# Then
You may don't care but those 2 values may or may not be the same because you access system time twice, one after another.
I would use this:
Code:
Private Sub Timer1_Timer()
Dim CurTime as Date
CurTime = Time
If CurTime > #1:00:00 AM# And CurTime < #6:00:00 AM# Then
Unload Me
End If
End Sub
or
Code:
Private Sub Timer1_Timer()
Select Case Time
Case Is <= #1:00:00 AM#
Case Is >= #6:00:00 AM#
Case Else: Unload Me
End Select
End Sub
Re: Find out a time between two times
Quote:
Originally Posted by anhn
Time value is not String, it is Date data type. You can check that with
Debug.Print TypeName(Time)
However with expression
Time > "1:00:00 AM",
because the RHS is a String so the LHS will be converted to a String before comparison occurs.
Eg., with Time = #10:23:15 AM#, the above expression will become
"10:23:15 AM" > "1:00:00 AM".
Because the 1st characters "1" = "1" but the 2nd characters "0" < ":", so
"10:23:15 AM" > "1:00:00 AM" is FALSE and
"10:23:15 AM" < "6:00:00 AM" is TRUE (because "1" < "6")
Instead of:
Time > "1:00:00 AM",
you can use:
Time > TimeSerial(1, 0, 0) as Merri suggested
or you can also use:
Time > TimeValue("1:00:00 AM"),
or perhaps easier:
Time > #1:00:00 AM#,
Your code can be written as:
Code:
Private Sub Timer1_Timer()
If Time > #1:00:00 AM# And Time < #6:00:00 AM# Then
Unload Me
End If
End Sub
Noted that there are 2 Time values in the line
If Time > #1:00:00 AM# And Time < #6:00:00 AM# Then
You may don't care but those 2 values may or may not be the same because you access system time twice, one after another.
I would use this:
Code:
Private Sub Timer1_Timer()
Dim CurTime as Date
CurTime = Time
If CurTime > #1:00:00 AM# And CurTime < #6:00:00 AM# Then
Unload Me
End If
End Sub
or
Code:
Private Sub Timer1_Timer()
Select Case Time
Case Is <= #1:00:00 AM#
Case Is >= #6:00:00 AM#
Case Else: Unload Me
End Select
End Sub
hi all
if time value is not astring . so you vb retuen the chat":" in the returned value. like :
time="10:10:10"
Re: Find out a time between two times
will.
this code is work'n. itry it
u can use this code and it's work.
if time > format("1:00:00 ","hh") or time < format("6:00:00"."hh") then
end
end if
Re: Find out a time between two times
I'm afraid you've got some learning to do yosef - you have misunderstood how Date/Time values work, which is a fairly made a common mistake.
Values for time and date (which are both stored in a Date data type) are not stored anything like you see them - they are stored as numbers (eg: 35456.234234) and are automatically formatted for you when you look at them in a Watch window etc.
Like other data types, they are also automatically converted when assigned to a String (such as Label1.Caption) or compared to a String (as anhn showed), and interpretation takes place when a String is passed to a function that expects a Date (such as format("1:00:00","hh") ).
The way that the values are formatted (unless you have explicitly used the Format function) or interpreted is determined by your Regional Settings at that moment - and can be changed by going into Control Panel. The : character does not need to be involved in times, and the format of dates can vary wildly.
This is all explained (with checks you can do yourself) in the article I linked to back in post #9
You should also read the article about End, as that is an evil statement which can cause big problems.
Re: Find out a time between two times
Thanks for all,
And done good and effective discussions.
My problem is solved
Nasreen