Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi,

I want to validate the time, Whether the entered time is falls between the two times using C#

Any Thoughts??

Thanks,
Priya
Posted
Updated 3-Jul-19 3:49am
Comments
AspDotNetDev 21-Jun-13 19:14pm    
Have you tried using DateTime? It supports comparison operators.
Sergey Alexandrovich Kryukov 21-Jun-13 22:28pm    
Thoughts? What do you mean by "thoughts"? Do you mean the brain process which happens while reading standard documentation? OK, and what did you try so far?
—SA

Use the type System.DateTime, which supports the operators: '<', '<=', '>', '>=' and '=='.

—SA
 
Share this answer
 
C#
// convert everything to TimeSpan
TimeSpan start = new TimeSpan(22, 0, 0);
TimeSpan end = new TimeSpan(07, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
// see if start comes before end
if (start < end)
    return start <= now && now <= end;
// start is after end, so do the inverse comparison
return !(end <= now && now <= start);


I Think It Is Helpful....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900