有没有一种文化安全的方式来获得ToShortDateString()和ToShortTimeString()带前导零? [英] Is there a culture-safe way to get ToShortDateString() and ToShortTimeString() with leading zeros?

查看:176
本文介绍了有没有一种文化安全的方式来获得ToShortDateString()和ToShortTimeString()带前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用格式字符串,但我不希望失去的日期/时间格式的特定文化的代表性。 。例如

I know that I could use a format string, but I don't want to lose the culture specific representation of the date/time format. E.g.

2011/5/4 |下午2时06分| ... 05/04/2011 |下午2时06分| ...

但是,当我将其更改为不同的文化,我想这是

But when I change it to a different culture, I want it to be

04.05.2011 | 14:06 | ...

不改变格式字符串。 ?这是可能的。

without changing the format string. Is that possible?

推荐答案

我看到的只是一个单一的解决方案 - 你应该获得当前区域性的显示格式,修补它,使它符合您的要求,最后使用修补格式字符串格式化日期时间值。下面是一些示例代码:

I see only a single solution - you should obtain the current culture display format, patch it so that it meets your requirement and finally format your DateTime value using the patched format string. Here is some sample code:

string pattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
        DateTime dt = DateTime.Now;
        string[] format = pattern.Split(new string[] { CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator }, StringSplitOptions.None);
        string newPattern = string.Empty;
        for(int i = 0; i < format.Length; i++) {
            newPattern = newPattern + format[i];
            if(format[i].Length == 1)
                newPattern += format[i];
            if(i != format.Length - 1)
                newPattern += CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
        }



作为承诺,这里是改良版:

As promised, here is the improved version:

/// <summary>
/// Extensions for the <see cref="DateTime"/> class.
/// </summary>
public static class DateTimeExtensions
{
    /// <summary>
    /// Provides the same functionality as the ToShortDateString() method, but
    /// with leading zeros.
    /// <example>
    /// ToShortDateString: 5/4/2011 |
    /// ToShortDateStringZero: 05/04/2011
    /// </example>
    /// </summary>
    /// <param name="source">Source date.</param>
    /// <returns>Culture safe short date string with leading zeros.</returns>
    public static string ToShortDateStringZero(this DateTime source)
    {
        return ToShortStringZero(source,
            CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern,
            CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator);
    }

    /// <summary>
    /// Provides the same functionality as the ToShortTimeString() method, but
    /// with leading zeros.
    /// <example>
    /// ToShortTimeString: 2:06 PM |
    /// ToShortTimeStringZero: 02:06 PM
    /// </example>
    /// </summary>
    /// <param name="source">Source date.</param>
    /// <returns>Culture safe short time string with leading zeros.</returns>
    public static string ToShortTimeStringZero(this DateTime source)
    {
        return ToShortStringZero(source,
            CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern,
            CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator);
    }

    private static string ToShortStringZero(this DateTime source, 
        string pattern,
        string seperator)
    {
        var format = pattern.Split(new[] {seperator}, StringSplitOptions.None);

        var newPattern = string.Empty;

        for (var i = 0; i < format.Length; i++)
        {
            newPattern = newPattern + format[i];
            if (format[i].Length == 1)
                newPattern += format[i];
            if (i != format.Length - 1)
                newPattern += seperator;
        }

        return source.ToString(newPattern, CultureInfo.InvariantCulture);
    }
}

这篇关于有没有一种文化安全的方式来获得ToShortDateString()和ToShortTimeString()带前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆