Processing time without date in TextBox

I am working on a time sheet application where I want the user to be able to enter time in TextBoxes, for example: 8 or 8:00 a or the like, as you can in Excel.

Now, if you enter a date in a TextBox and then use DateTime.TryParse, you can enter it in several formats (January 31, 2007, January 31, 2007, January 31, 2007, January 31, 2007).), And .NET will figure this out and turn it into a DateTime.

But when I use DateTime.TryParse for a string like "8 a" or "8:00 a", she does not understand it.

I know I can use ParseExact, but I wonder if there is a more flexible solution. I want .NET to get 8: 00a from "8 a" or "8:00 a" and leave the default date component 1/1/0001.

+4
source share
4 answers

Think about it, never mind. I just noticed that if I use "am" and "pm" instead of "a" and "p", it works fine. It takes a date today, not the default 1/1/0001, but that is not a problem for my purposes.

(However, any reasonable solution to get "a" and "p" to work is welcome.)

+3
source

You can always spoof the system (quite easily):

DateTime blah = DateTime.Parse("1/1/0001 " + myTimeString); 

I did something similar to myself.

+4
source

I would review your user interface functionality and replace it with two combined fields or some other form of timing.

Ultimately, even the best time analyzer in the world will still fail to test the idiot.

Banning Parse.Exact is what you need.

+3
source

You can use a validation or confirmed event to capture text and add "m".

0
source

All Articles