Results 1 to 6 of 6

Thread: check time - hour and minute only - new problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    check time - hour and minute only - new problem

    Hi,

    How can I validate a time to ensure that the hour and minute are not of the past.

    I tried someting lile this

    Code:
    (dtpFrom.Value.TimeOfDay) < (DateTime.Now.TimeOfDay)
    but this is also looking at seconds. I want to check only whether the hour and minute is of the past and not the seconds.

    Any help
    Last edited by sunshine123; Apr 16th, 2007 at 11:24 AM. Reason: new problem

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: check time - hour and minute only

    Try this,
    C# Code:
    1. if (dtpFrom.Value.ToShortTimeString().Compare(DateTime.Now.ToShortTimeString()) == -1)
    2. {
    3.     //Do Something
    4. }

    EDIT: Is dtpFrom a DateTimePicker class?? Isn't this value, dtpFrom.Value, return the current time??
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: check time - hour and minute only

    Quote Originally Posted by Harsh Gupta
    Try this,
    C# Code:
    1. if (dtpFrom.Value.ToShortTimeString().Compare(DateTime.Now.ToShortTimeString()) == -1)
    2. {
    3.     //Do Something
    4. }

    EDIT: Is dtpFrom a DateTimePicker class?? Isn't this value, dtpFrom.Value, return the current time??

    Thanks..... and also it should be CompareTo instead of Compare

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: check time - hour and minute only - new problem

    Quote Originally Posted by sunshine123
    Thanks..... and also it should be CompareTo instead of Compare
    But it does not work when the hour is 10, 11 or 12. Why is that.

    say for example if the current time is 3.30 pm, I have selected 2.30 pm then it identifies it as a past time. But if I have selected 10.30pm it still is calculated as a past time.

    Any help please..??
    Last edited by sunshine123; Apr 16th, 2007 at 11:22 AM.

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: check time - hour and minute only - new problem

    Sorry, my mistake, the CompareTo() works only with strings and the ToShortTimeString() returns a string, and so it compares the first non matching symbol in the 2 strings and checks for precedence. 1 comes before 2,3, hence it doesn't work for you.

    Check for DateTime.Compare(DateTime, DateTime) method or DateTime.CompareTo(DateTime) method.
    Show Appreciation. Rate Posts.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: check time - hour and minute only - new problem

    Thank you Harsh for the links ...It just worked perfectly

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