Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 24 hours number from System.DateTime.Now.Hour? [closed]

Tags:

c#

datetime

I want to get the current hour number with 0-24 from System.DateTime.Now.Hour. I am not sure if it will return an integer between 0-12 or 0-24. How do I make sure I will get an integer between 0-24?

like image 346
KentZhou Avatar asked Dec 12 '12 19:12

KentZhou


2 Answers

DateTime.Hour Property

The value of the Hour property is always expressed using a 24-hour clock. To retrieve a string that represents the hour of a date and time using a 12-hour clock, call the DateTime.ToString(String) or DateTime.ToString(String, IFormatProvider) method with the "h" custom format specifier. For example:

and

The hour component, expressed as a value between 0 and 23.

like image 61
Tim Schmelter Avatar answered Nov 20 '22 20:11

Tim Schmelter


If you are trying to get a specific string version of a DateTime object I suggest using either a custom date and time format string or a standard date and time format string.

I usually use dateTimeVar.ToString("yyyy/MM/dd HH:mm:ss.ffff zzz"); though you may want K instead of zzz

like image 1
Trisped Avatar answered Nov 20 '22 20:11

Trisped