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:o
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??
Re: check time - hour and minute only
Quote:
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
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..??:o
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.
Re: check time - hour and minute only - new problem
Thank you Harsh for the links ...It just worked perfectly:)