views:

74

answers:

4

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 of 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);

Thanks!

+1  A: 

It might not be pretty but if they insist

if (Locality == france)
myDateTime.ToString(Resources.Strings.CustomTimeFormat).Replace("00","") ;
else 
myDateTime.ToString(Resources.Strings.CustomTimeFormat);
Roadie57
That is not pretty, but I don't think there is another choice :/
GôTô
+3  A: 
code4life
@code4life. Big points for your answer its very well written and I love the included tests. I did post my own answer which is a little more generic to hopefully accommodate all custom DateTime formats the user may wish to use.
Justin
+2  A: 

Following on code4life's extension method idea, here's an extension method. =p

public static string ToCanadianTimeString(this DateTime source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    if (source.Minute > 0)
        return String.Format("{0:hh} h {0:mm}", source);

    else
        return String.Format("{0:hh} h", source);
}
Moudis
@Moudis: you should make it culture-sensitive...
code4life
Muhahaha, it is: "ToCanadianTimeString".
Hans Passant
@code4life: I can think of a couple of ways to either query the current culture or pass in a culture. I'll update the example to one of those a bit later if that's what you mean.
Moudis
If I have to revert to an extension method it should handle ALL cultures. Perhaps have 2 localized formats Strings.NoShowMinutesTimeFormat and Strings.HasMinutesTimeForm. The problem with this is that sometimes the Time is combined with a Date. So the format include something like "dddd d MMMM yyyy" whereas other parts of the app may use "d MMMM yyyy". Looks like I'll have to edit the format to remove "mm" from the format when the culture is fr-ca and the minutes are zero. I'll post my solution soon.
Justin
Man, that was inspired, ToCanadianTimeString...!
code4life
A: 

Here is the solution I'm going with:

public static string ToStringOverride(this DateTime dateTime, string format)
{
    // Adjust the "format" as per unique Culture rules not supported by the Framework
    if (CultureInfo.CurrentCulture.LCID == 3084) // 3084 is LCID for fr-ca
    {
        // French Canadians do NOT show 00 for minutes.  ie.  8:00 is shown as "8 h" not "8 h 00"
        if (dateTime.Minute == 0)
        {
            format = format.Replace("mm", string.Empty);
        }
    }

    return dateTime.ToString(format);
}

It will at least work for different DateTime formats such as...

  • H \h mm
  • dddd d MMMM yyyy H \h mm tt

The idea here is I modify the FormatString to remove "mm" if minutes are zero. Still letting the framework do the hard work.

Justin