PDA

Click to See Complete Forum and Search --> : check time - hour and minute only - new problem


sunshine123
Apr 15th, 2007, 09:06 AM
Hi,

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

I tried someting lile this



(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

Harsh Gupta
Apr 15th, 2007, 09:27 AM
Try this,
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??

sunshine123
Apr 15th, 2007, 09:55 AM
Try this,
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

sunshine123
Apr 16th, 2007, 11:16 AM
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

Harsh Gupta
Apr 16th, 2007, 12:28 PM
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 (http://msdn2.microsoft.com/en-us/library/system.datetime.compare.aspx) or DateTime.CompareTo(DateTime) method (http://msdn2.microsoft.com/en-us/library/5ata5aya.aspx).

sunshine123
Apr 17th, 2007, 11:58 AM
Thank you Harsh for the links ...It just worked perfectly:)