Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Time Formatting. Localization to French how do I get the output "5h 45" for 5:45?

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);
like image 841
Justin Avatar asked Oct 08 '10 19:10

Justin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

I have some bad news for you. The framework does not support the format you are looking for. The following code proves this:

using System;
using System.Globalization;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            // FR Canadian
            Console.WriteLine("Displaying for: fr-CA");
            DisplayDatesForCulture("fr-CA");

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            // FR French
            Console.WriteLine("Displaying for: fr-FR");
            DisplayDatesForCulture("fr-FR"); 

            Console.WriteLine();
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        static void DisplayDatesForCulture(string culture)
        {
            var ci = CultureInfo.GetCultureInfo(culture);
            var dt = new DateTime(2010, 10, 8, 18, 0, 0);

            foreach (string s in ci.DateTimeFormat.GetAllDateTimePatterns())
                Console.WriteLine(dt.ToString(s));
        }
    }
}

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:

public static class DateHelper
{
    public static string ToLocalizedLongTimeString(this DateTime target)
    {
        return ToLocalizedLongTimeString(target, CultureInfo.CurrentCulture);
    }

    public static string ToLocalizedLongTimeString(this DateTime target, 
        CultureInfo ci)
    {
        // I'm only looking for fr-CA because the OP mentioned this 
        // is specific to fr-CA situations...
        if (ci.Name == "fr-CA")
        {
            if (target.Minute == 0)
            {
                return target.ToString("H' h'");
            }
            else
            {
                return target.ToString("H' h 'mm");
            }
        }
        else
        {
            return target.ToLongTimeString();
        }
    }
}

You can test like so:

var dt = new DateTime(2010, 10, 8, 18, 0, 0);

// this line will return 18 h
Console.WriteLine(dt.ToLocalizedLongTimeString(CultureInfo.GetCultureInfo("fr-CA")));

// this line returns 6:00:00 PM
Console.WriteLine(dt.ToLocalizedLongTimeString());

var dt2 = new DateTime(2010, 10, 8, 18, 45, 0);

// this line will return 18 h 45
Console.WriteLine(dt2.ToLocalizedLongTimeString(CultureInfo.GetCultureInfo("fr-CA")));

// this line returns 6:45:00 PM
Console.WriteLine(dt2.ToLocalizedLongTimeString());
like image 94
code4life Avatar answered Sep 27 '22 23:09

code4life


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);
}
like image 44
Moudis Avatar answered Sep 27 '22 23:09

Moudis