Results 1 to 16 of 16

Thread: Find out a time between two times

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    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

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  3. #3
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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
    IT CTO & System Administrator.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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)
    Last edited by Siddharth Rout; Nov 19th, 2008 at 01:03 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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.
    IT CTO & System Administrator.

  8. #8
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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
    IT CTO & System Administrator.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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).

  11. #11
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    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
    Last edited by Caskbill; Nov 19th, 2008 at 04:50 PM.

  12. #12
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  13. #13
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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"
    IT CTO & System Administrator.

  14. #14
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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
    IT CTO & System Administrator.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    105

    Resolved Re: Find out a time between two times

    Thanks for all,
    And done good and effective discussions.
    My problem is solved

    Nasreen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width