Reputation: 635
I have a date like this:
equipBooking.BookedFromDteTme.Date = `{12/5/2013 12:00:00 AM}`
and I want to apply time like this, but I do not know how to get AM and PM from the end.
dtStartTimeHour.SelectedItem = equipBooking.BookedFromDteTme.TimeOfDay.Hours;
dtStartTimeMin.SelectedItem = equipBooking.BookedFromDteTme.TimeOfDay.Minutes;
**dtStartTimeAMPM.SelectedItem = equipBooking.BookedFromDteTme.???????.;**
Please help me.
I have tried something like this:
var startDatestr = equipBooking.BookedFromDteTme.TimeFrom.Split(new string[] { ":", ":",":",":" }, StringSplitOptions.RemoveEmptyEntries);
AM/PM = startDatestr[3]
Upvotes: 1
Views: 9331
Reputation: 152566
If you just want the string you can do:
equipBooking.BookedFromDteTme.ToString("tt");
Or for a boolean result use:
bool isPM = (equipBooking.BookedFromDteTme.Hour >= 12);
BTW, you don't need to call TimeOfDay
- you can get the Hour
and Minute
property directly from the DateTime
:
dtStartTimeHour.SelectedItem = equipBooking.BookedFromDteTme.Hour;
dtStartTimeMin.SelectedItem = equipBooking.BookedFromDteTme.Minute;
dtStartTimeAMPM.SelectedItem = equipBooking.BookedFromDteTme.ToString("tt");
Upvotes: 7
Reputation: 887449
TimeSpan
does not store time-of-day, but rather the length of any interval of time. It has no notion of AM/PM.
TimeOfDay
returns the amount of time since midnight.
If Hours
is more than or equal to 12, that will be PM.
Upvotes: 3