none
check is current time is lie between two times "t1" and "t2" RRS feed

  • Question

  •  

    hi,
    I am working in vb.net 2005

    i want to check is current time is lie between two times "t1" and "t2"
    i want to check only time values not a complete date or with date.

    for example

    Current time= "06:21:18 PM"

    t1="08:30:15 PM"
    t2 ="02:10:05 AM"

    WAITING FOR REPLY

    Thursday, February 12, 2009 7:32 PM

Answers

  • If the range is within a 24-hour timespan see if the following works for you:

            Dim StartTime As Date = #10:15:15 PM#  
            Dim EndTime As Date = #2:15:15 PM#  
            Dim CurrentTime As Date = #8:27:00 PM#  
     
            If EndTime.Ticks < StartTime.Ticks Then 
                'EndTime is time for next day  
                If (CurrentTime.Ticks >= StartTime.Ticks And CurrentTime.Ticks >= EndTime.Ticks) Or _  
                    (CurrentTime.Ticks <= StartTime.Ticks And CurrentTime.Ticks <= EndTime.Ticks) Then 
                    Console.WriteLine("Time is within range.")  
                Else 
                    Console.WriteLine("Time is outside of range.")  
                End If 
            Else 
                If CurrentTime.Ticks >= StartTime.Ticks And CurrentTime.Ticks <= EndTime.Ticks Then 
                    Console.WriteLine("Time is within range.")  
                Else 
                    Console.WriteLine("Time is outside of range.")  
                End If 
            End If 

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, February 13, 2009 8:00 PM
  • 'the code i posted works. 
     
            'works only with times - included dates to prove it 
            Dim StartTime As DateTime = #7/29/1954 2:25:25 AM# 'this should be before EndTime 
            Dim EndTime As DateTime = #2/5/1975 6:00:15 PM# 'this should be after StartTime 
     
            Dim TSendTM As TimeSpan = EndTime.TimeOfDay 'get only the time portion  
            Dim TSstrtTM As TimeSpan = StartTime.TimeOfDay 'for start and end 
     
            Dim CurrentTime As TimeSpan = DateTime.Now.TimeOfDay 
     
            If CurrentTime.TotalMilliseconds >= TSstrtTM.TotalMilliseconds AndAlso _ 
                CurrentTime.TotalMilliseconds <= TSendTM.TotalMilliseconds Then 
                Debug.WriteLine("Time is within range."
            End If 
     


    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Saturday, February 14, 2009 12:01 AM

All replies

  •         Dim t1 As Date = #4:30:15 PM#  
            Dim t2 As Date = #2:10:05 AM#  
            Dim CurrentTime As DateTime = Convert.ToDateTime(DateTime.Now)  
     
            If CurrentTime.TimeOfDay.Ticks >= t2.Ticks And CurrentTime.TimeOfDay.Ticks <= t1.Ticks Then 
                Console.WriteLine("Time is within range.")  
            End If 

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, February 12, 2009 8:11 PM
  • Dim t1 As Date = "7:31:15 PM"

    Dim t2 As Date = "2:25:25 AM"

    Dim CurrentTime As DateTime = Convert.ToDateTime(DateTime.Now)

    If CurrentTime.TimeOfDay.Ticks >= t2.Ticks And CurrentTime.TimeOfDay.Ticks <= t1.Ticks Then

    MessageBox.Show("Time is within range.")

    End IF

    -------------------------------------------------------------------------------

    The above code id not working
    please check this data. for example:

    t1 = #7:20:15 PM#
    t2 = #2:25:25 AM#

    Current time = #8:27 PM#


    Now it  should to return True means Current time lie between TI and T2 but it returns false mean not Between t1 and t2
    Time range should be from t1 to t2.Not from t2 to t1

    pleae reply soon

    Friday, February 13, 2009 4:37 PM
  • Option Strict On

    should be the first line of your program!


            Dim t1 As DateTime = #7:31:15 PM# 
            Dim t2 As DateTime = #2:25:25 AM# 
            Dim t1T As TimeSpan = t1.TimeOfDay 'get the time portion 
            Dim t2T As TimeSpan = t2.TimeOfDay 
     
            Dim CurrentTime As TimeSpan = DateTime.Now.TimeOfDay 
     
            If CurrentTime.TotalMilliseconds >= t2T.TotalMilliseconds AndAlso _ 
                CurrentTime.TotalMilliseconds <= t1T.TotalMilliseconds Then 
                MessageBox.Show("Time is within range."
            End If 
     


    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Friday, February 13, 2009 4:58 PM
  • Naeem Ul Hussan said:

    t1 = #7:20:15 PM#
    t2 = #2:25:25 AM#

    Current time = #8:27 PM#


    Now it  should to return True means Current time lie between TI and T2 but it returns false mean not Between t1 and t2
    Time range should be from t1 to t2.Not from t2 to t1

    pleae reply soon




    No, I'm afraid it's False unless you include the date portion in the evaluation. If you don't know the date, or can't assume the date is the current day then there is no way you can evaluate this expression accurately.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, February 13, 2009 5:26 PM
  • The above code is working good but failed when change the value of
    t1. for example 

    First in code the value of

    Dim t1 As DateTime = #7:31:15 PM#

    Dim t2 As DateTime = #2:25:25 AM#

    and current time is 8:25:13 PM

    it shows the current time is within t1 and t2

    Now change the value of t1 ="10:31:15 PM"  ,  value of t2 not changed  and current time is  8:40 :15 PM

    Now it should to show the current time not between t1 and t2 but it shoes yes

    Please reply what is problem

    Friday, February 13, 2009 5:50 PM
  • I am working on a Gift Draw application. i want some specific gifts only should to include in the draw daily when the time is between night #10:15:15 PM# and next morning#2:15:15 AM# otherwise normal gift should to include in draw.

    for example.

    Gift name = Tv

    i want, Tv should to include in draw Gifts only when time is between above  t1 and t2

    please give me the solution of this problem

    • Edited by Naeem Ul Hussan Saturday, February 14, 2009 6:39 AM due to some change in time AM ,PM,please review my problem again
    Friday, February 13, 2009 6:01 PM
  • If the range is within a 24-hour timespan see if the following works for you:

            Dim StartTime As Date = #10:15:15 PM#  
            Dim EndTime As Date = #2:15:15 PM#  
            Dim CurrentTime As Date = #8:27:00 PM#  
     
            If EndTime.Ticks < StartTime.Ticks Then 
                'EndTime is time for next day  
                If (CurrentTime.Ticks >= StartTime.Ticks And CurrentTime.Ticks >= EndTime.Ticks) Or _  
                    (CurrentTime.Ticks <= StartTime.Ticks And CurrentTime.Ticks <= EndTime.Ticks) Then 
                    Console.WriteLine("Time is within range.")  
                Else 
                    Console.WriteLine("Time is outside of range.")  
                End If 
            Else 
                If CurrentTime.Ticks >= StartTime.Ticks And CurrentTime.Ticks <= EndTime.Ticks Then 
                    Console.WriteLine("Time is within range.")  
                Else 
                    Console.WriteLine("Time is outside of range.")  
                End If 
            End If 

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, February 13, 2009 8:00 PM
  • 'the code i posted works. 
     
            'works only with times - included dates to prove it 
            Dim StartTime As DateTime = #7/29/1954 2:25:25 AM# 'this should be before EndTime 
            Dim EndTime As DateTime = #2/5/1975 6:00:15 PM# 'this should be after StartTime 
     
            Dim TSendTM As TimeSpan = EndTime.TimeOfDay 'get only the time portion  
            Dim TSstrtTM As TimeSpan = StartTime.TimeOfDay 'for start and end 
     
            Dim CurrentTime As TimeSpan = DateTime.Now.TimeOfDay 
     
            If CurrentTime.TotalMilliseconds >= TSstrtTM.TotalMilliseconds AndAlso _ 
                CurrentTime.TotalMilliseconds <= TSendTM.TotalMilliseconds Then 
                Debug.WriteLine("Time is within range."
            End If 
     


    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Saturday, February 14, 2009 12:01 AM