How to compare time?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    #1

    How to compare time?

    Hi Guys,

    Just looking for the best way to compare two times.

    What I want to do is make sure a time is in between two other times.

    E.g. If the current time is 14.00 I want to check that it is between 13.30
    and 14.30.

    Would it be best be using the TimeSpan class?

    Idealy I want to do:

    If (Time1 > Time2) And (Time1 < Time3) then

    'do stuff

    End If

    but that does not work correctly.

    Thanks

    James


  • Cor Ligthert [MVP]

    #2
    Re: How to compare time?

    James,

    The ideas about your question are different in this newsgroups.

    In my idea if you only want to compare without knowing anything exact, than
    the ticks can do everting for you (is a long value).

    (The result value of ticks says almost nothing it started at 1-1-1 while the
    calandars have changed during that time).

    I hope this helps,

    Cor

    "James" <ram@iexpress.n et.au> schreef in bericht
    news:eeQjAexUGH A.196@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Hi Guys,
    >
    > Just looking for the best way to compare two times.
    >
    > What I want to do is make sure a time is in between two other times.
    >
    > E.g. If the current time is 14.00 I want to check that it is between 13.30
    > and 14.30.
    >
    > Would it be best be using the TimeSpan class?
    >
    > Idealy I want to do:
    >
    > If (Time1 > Time2) And (Time1 < Time3) then
    >
    > 'do stuff
    >
    > End If
    >
    > but that does not work correctly.
    >
    > Thanks
    >
    > James
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: How to compare time?

      "James" <ram@iexpress.n et.au> schrieb:[color=blue]
      > What I want to do is make sure a time is in between two other times.
      >
      > E.g. If the current time is 14.00 I want to check that it is between 13.30
      > and 14.30.
      >
      > Would it be best be using the TimeSpan class?
      >
      > Idealy I want to do:
      >
      > If (Time1 > Time2) And (Time1 < Time3) then
      >
      > 'do stuff
      >
      > End If
      >
      > but that does not work correctly.[/color]


      What exactly does not work correctly?

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: How to compare time?

        James,
        Generally I use a TimeRange class.



        Dim range As New TimeRange(#13:3 0:00 PM#, #14:30:00 AM#)

        If range.Contains( Time1) then
        'do stuff
        End If

        Although TimeRange accepts DateTime parameters, internally it is represented
        as TimeSpan objects.

        (Yes I need to update the descriptions on that page).

        --
        Hope this helps
        Jay [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        "James" <ram@iexpress.n et.au> wrote in message
        news:eeQjAexUGH A.196@TK2MSFTNG P10.phx.gbl...
        | Hi Guys,
        |
        | Just looking for the best way to compare two times.
        |
        | What I want to do is make sure a time is in between two other times.
        |
        | E.g. If the current time is 14.00 I want to check that it is between 13.30
        | and 14.30.
        |
        | Would it be best be using the TimeSpan class?
        |
        | Idealy I want to do:
        |
        | If (Time1 > Time2) And (Time1 < Time3) then
        |
        | 'do stuff
        |
        | End If
        |
        | but that does not work correctly.
        |
        | Thanks
        |
        | James
        |
        |


        Comment

        Working...