1

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]
rgettman
  • 176,041
  • 30
  • 275
  • 357
patel
  • 635
  • 5
  • 22
  • 40
  • If the hours are less than 12, I guess it would be AM, otherwise PM. – itsme86 Dec 09 '13 at 19:25
  • 1
    duplicate of: http://stackoverflow.com/questions/7875259/how-get-a-m-p-m-from-datetime – Maess Dec 09 '13 at 19:26
  • Possible duplicate of [How do I get the AM/PM value from a DateTime?](https://stackoverflow.com/questions/7875259/how-do-i-get-the-am-pm-value-from-a-datetime) – dey.shin Oct 20 '17 at 18:19

3 Answers3

7

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");
D Stanley
  • 149,601
  • 11
  • 178
  • 240
3

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

try using dateTime.ToString("tt");

kitensei
  • 2,510
  • 2
  • 42
  • 68