Re: Convert hh:mm:ss AM/PM into valid time

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Try the Parse methods in the DateTime class

[VB.Net]
Dim date1 = DateTime.Parse("12:34:56 PM")
Dim date2 = DateTime.ParseExact("12:34:56 PM", "hh:mm:ss tt", Nothing)

[C#]
DateTime date1 = DateTime.Parse("12:34:56 PM");
DateTime date2 = DateTime.ParseExact("12:34:56 PM", "hh:mm:ss tt", null);

Keep in mind that the DateTime structure will have the date component
default to today's date.
The documentation suggests that the ParseExact expects a perfect match -
which might be a bit demanding for an open text field. Maybe with an input
mask...

Incidentally, I (nearly) always use the DateTimePicker control in the UI for
dates.
If the date is optional, I enable the check box.
Since DateTime is structure and not a class, it can't be null - I store null
dates as DateTime.MinValue.

I hope that helps.

Arnd Hurlbrink



<thrikris@xxxxxxxxx> wrote in message
news:1138723302.238934.285030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I am using an edit control to accept time including AM/PM. I can get
the hour/min and seconds but how do I convert the AM/PM part into the
internal representation for time using a DateTime object.
Any help appreciated.



.


Quantcast