|
-
Apr 15th, 2007, 09:06 AM
#1
Thread Starter
Lively Member
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
-
Apr 15th, 2007, 09:27 AM
#2
Re: check time - hour and minute only
Try this,
C# Code:
if (dtpFrom.Value.ToShortTimeString().Compare(DateTime.Now.ToShortTimeString()) == -1)
{
//Do Something
}
EDIT: Is dtpFrom a DateTimePicker class?? Isn't this value, dtpFrom.Value, return the current time??
-
Apr 15th, 2007, 09:55 AM
#3
Thread Starter
Lively Member
Re: check time - hour and minute only
 Originally Posted by Harsh Gupta
Try this,
C# Code:
if (dtpFrom.Value.ToShortTimeString().Compare(DateTime.Now.ToShortTimeString()) == -1)
{
//Do Something
}
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
-
Apr 16th, 2007, 11:16 AM
#4
Thread Starter
Lively Member
Re: check time - hour and minute only - new problem
 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.
-
Apr 16th, 2007, 12:28 PM
#5
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.
-
Apr 17th, 2007, 11:58 AM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|