when i get the DateTime.Hour property, i always get the 24 hour time (so 6PM would give me 18).
how do i get the "12 hour" time so 6PM just gives me 6.
i obviously can do the check myself but i assume there is a built in function for this.
when i get the DateTime.Hour property, i always get the 24 hour time (so 6PM would give me 18).
how do i get the "12 hour" time so 6PM just gives me 6.
i obviously can do the check myself but i assume there is a built in function for this.
How about:
DateTime.Hour % 12
That will give 0-11 of course... do you want 1-12? If so:
((DateTime.Hour + 11) % 12) + 1
I don't think there's anything simpler built in...
There's no built-in function, mainly because you shouldn't need it:
Outside of this, the math calculation is simple enough and already available in other answers here.
DateTime.Now.ToString("hh"); --> Using this you will get "06" for 18h.
I don't know of any built in method, but you can always add an extension method to accomplish this.
Of course, you could always replace the code with the way you want to accomplish it.
public static class Extension
{
public static int GetTwelveCycleHour(this DateTime dateTime)
{
if (dateTime.Hour > 12)
{
return dateTime.Hour - 12;
}
return dateTime.Hour;
}
}