C#时间格式。本地化为法语如何获得输出“ 5h 45” 5:45?

我正在尝试为法国客户设置日期格式。 我需要格式化Times,如以下示例所示...... 06:00 - > 6 h 08:45 - > 8小时45 10:30 - > 10 h 30 15:00 - > 15 h 17:22 - > 17 h 22 18:00 - > 18 h 我已经能够使用自定义日期和时间格式。但我似乎仍然坚持这种表示法国(至少加拿大人)如果他们是零“00”则不显示会议纪要。 目前我使用以下格式。
myDateTime.ToString("H h mm")
如何才能使“mm”仅在> 00时出现? 我想避免使用扩展方法或代理类,因为我觉得这应该构建到框架中。显然是法国时代的标准格式。 我的字符串“H h mm”实际上来自资源文件。即...
myDateTime.ToString(Resources.Strings.CustomTimeFormat);
    
已邀请:
我有一些坏消息要告诉你。该框架不支持您要查找的格式。以下代码证明了这一点:
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));
        }
    }
}
该应用显示所有支持的日期时间格式。他们都没有支持18:00 ==> 18小时等的概念。 您最好的选择是编写扩展方法或类似方法。 文化敏感方法:构建扩展帮助类:
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();
        }
    }
}
你可以这样测试:
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());
    
遵循code4life的扩展方法思路,这是一个扩展方法。 = 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);
}
    
它可能不是很漂亮,但如果他们坚持的话
if (Locality == france)
myDateTime.ToString(Resources.Strings.CustomTimeFormat).Replace("00","") ;
else 
myDateTime.ToString(Resources.Strings.CustomTimeFormat);
    
这是我要去的解决方案:
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);
}
它至少适用于不同的DateTime格式,例如...... H h mm dddd d MMMM yyyy H h mm tt 这里的想法是我修改FormatString以在分钟为零时删除“mm”。仍然让框架做艰苦的工作。     
这也不是很好:
myDateTime.ToString(myDateTime.Minute == 0 ? @"H h" : @"H h mm")
    

要回复问题请先登录注册