Is there a way to keep the culture specific date time formatting but force 12/24 hour rendering? I know I can do a lot with the actual date/time format string like HH:mm:ss
and hh:mm:ss
but I would like to honor the current user culture formatting (i.e. mm/dd/yyyy
or yyyy/mm/dd
, etc), just force 12/24 hour time rendering.
Firstly, set the 12 hr format date. DateTime d = DateTime. Parse("05:00 PM"); Now let us convert it into 24-hr format.
DateTime date = DateTime. ParseExact(strDate, "dd/MM/YYYY HH:mm:ss", CultureInfo. InvariantCulture);
If UseUserOverride is true and the specified culture matches the current culture of Windows, the CultureInfo uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the ...
The pattern dd/MM/yyyy hh:mm:ss aa is used for the 12 hour format and the pattern MM-dd-yyyy HH:mm:ss is used for the 24 hour format.
I'd probably do something like this:
var culture = CultureInfo.CurrentCulture;
var pattern = culture.DateTimeFormat.LongTimePattern; // or pick which one you want to use;
var newPattern = pattern.Replace("h", "H").Replace("t", "");
DateTime.Now.ToString(newPattern); // or use whatever DateTime you want to use
From the chat:
Here is a list of all cultures' long time pattern strings, and how they would be modified:
Old: hh:mm:ss tt New: HH:mm:ss
Old: HH:mm:ss 'ч.' New: HH:mm:ss 'ч.'
Old: HH:mm:ss New: HH:mm:ss
Old: H:mm:ss New: H:mm:ss
Old: h:mm:ss tt New: H:mm:ss
Old: tt h:mm:ss New: H:mm:ss
Old: h:mm:ss.tt New: H:mm:ss.
Old: HH.mm.ss New: HH.mm.ss
Old: tt hh:mm:ss New: HH:mm:ss
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With