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

发布于 2024-09-26 17:12:21 字数 642 浏览 8 评论 0原文

我正在尝试为法国客户设置日期格式。

我需要格式化时间,如以下示例所示...

06:00 -> 6小时
08:45 --> 8 小时 45
10:30→ 10 小时 30
15:00-> 15小时
17:22 --> 17 小时 22
18:00-> 18 小时

我已经能够使用自定义日期和时间格式。但我似乎坚持这样一种观念,即法国人(至少加拿大人)如果分钟数为零“00”,则不会显示分钟数。

目前我正在使用以下格式。

myDateTime.ToString("H \h mm")

我怎样才能使“mm”仅在>时出现00?

我想避免使用扩展方法或代理类,因为我认为这应该内置到框架中。显然这是法国时间的标准格式。

我的字符串“H \h mm”实际上来自资源文件。 IE...

myDateTime.ToString(Resources.Strings.CustomTimeFormat);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Spring初心 2024-10-03 17:12:21

我有一些坏消息要告诉你。该框架支持您正在寻找的格式。以下代码证明了这一点:

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

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());
怀里藏娇 2024-10-03 17:12:21

遵循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);
}

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);
}
世俗缘 2024-10-03 17:12:21

它可能不漂亮,但如果他们坚持

if (Locality == france)
myDateTime.ToString(Resources.Strings.CustomTimeFormat).Replace("00","") ;
else 
myDateTime.ToString(Resources.Strings.CustomTimeFormat);

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);
泪意 2024-10-03 17:12:21

这是我要使用的解决方案:

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

它至少适用于不同的日期时间格式,例如...

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

这里的想法是我修改 FormatString 以删除“mm”如果分钟为零。仍然让框架来完成艰苦的工作。

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.

肥爪爪 2024-10-03 17:12:21

这也不漂亮:

myDateTime.ToString(myDateTime.Minute == 0 ? @"H \h" : @"H \h mm")

This is not pretty either:

myDateTime.ToString(myDateTime.Minute == 0 ? @"H \h" : @"H \h mm")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文