I’m attempting to format dates for a French customer.
I need to format Times as shown in the following examples…
06:00 -> 6 h
08:45 -> 8 h 45
10:30 -> 10 h 30
15:00 -> 15 h
17:22 -> 17 h 22
18:00 -> 18 h
I’ve been able to use Custom Date and Time Formatting. But I seem to be stuck on this notation that the French (Canadian at least) don’t show the Minutes if they are zero “00”.
Currently I’m using the following format.
myDateTime.ToString("H \h mm")
How can I make it so “mm” only appears when > 00?
I’d like to avoid the use of an extension method or proxy class since I feel this should be built into the framework. Apparently its standard formatting for French times.
My string “H \h mm” is actually coming from a resource file. ie…
myDateTime.ToString(Resources.Strings.CustomTimeFormat);
I have some bad news for you. The framework does not support the format you are looking for. The following code proves this:
The app displays all supported datetime formats. None of them support the concept of 18:00 ==> 18 h, etc.
Your best option is to write an extension method or similar approach.
Culture sensitive approach: build an extension helper class:
You can test like so: