I am creating a C# forms application and on that application needs to be an input box of some sort for users to type in a time in the format hh:mm:ss. This also needs to be able to be validated so that users cannot enter anything other than numbers, but also validated so that the hour has to be between 0-24, minutes between 0-60 and seconds between 0-60.
Someone suggested using three different text boxes (One for hours, minutes and seconds) but this, to me, seems archaic and more hassle than it's worth for validation as well.
I need to be able to use a statement to basically say if time is below 24:00:00, do x
Am I right in assuming DateTime
would be best in this scenario? I'm asking as I'm not 100% sure since I've never really used DateTime
before, only read about it.
Thank you.
If you're happy to take a dependency on a 3rd party library, I'd suggest using the date/time library I maintain: Noda Time. Unlike the .NET framework, it has a type specifically for a "time without a date" - LocalTime
.
You can use a LocalTimePattern
to convert between a LocalTime
and string
representations.
If you want to stick to the BCL, then either DateTime
or TimeSpan
will work - but neither type actually describes the kind of value you're trying to represent.
See more on this question at Stackoverflow