从完整的DateTime模式中提取Date模式 [英] Extracting just the Date pattern from a full DateTime pattern

查看:558
本文介绍了从完整的DateTime模式中提取Date模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在允许用户使用如下所示的完整模式设置其首选的DateTime格式:

I'm currently allowing users to set their preferred DateTime format using full patterns like so:

var formats = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns('f').ToList();
formats.AddRange(System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns('F'));
formats.AddRange(System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns('g'));
formats.AddRange(System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetAllDateTimePatterns('G'));

这样可以生成完整的模式字符串:

This produces full pattern strings like this:

dddd, MMMM d, yyyy h:mm tt
dddd, MMMM d, yyyy hh:mm tt
dddd, MMMM d, yyyy H:mm
dddd, MMMM d, yyyy HH:mm
MMMM d, yyyy h:mm tt
dddd, d MMMM, yyyy h:mm:ss tt
M/d/yyyy h:mm tt

在UI中有一些我们不想显示时间的地方,因为它

There are certain places in the UI where we don't want to show the Time because its irrelevant in context.

是否有一种文化感知的BCL类,允许我从字符串中提取Date模式?

示例api:

var datePattern = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.GetDatePattern("M/d/yyyy h:mm tt");

这将导致将datePattern 设置为:

Which would result in datePattern being set to:

M/d/yyyy






更新

基于@SteveWellens评论,我意识到日期/时间模式是不是在自己的文化变体,这是什么扔我。换句话说,无论最终用户使用什么文化,代表小时占位符的h总是h。我创建了一个简单的扩展方法来获得我想要的。 (希望这不一定是一个字符串扩展,因为它会阻塞智能感知)

Based on @SteveWellens comment I realized that Date/Time patterns are not in themselves culture variant which is what was throwing me off. In other words the "h" that represents a placeholder for the hour is always "h" no matter what culture the end user is in. I created a dead simple extension method to get what I wanted. (wish this didn't have to be a string extension though because it clutters intellisense)

public static string GetDatePattern(this string pattern)
{
    // set a default pattern if none is specified
    if (string.IsNullOrWhiteSpace(pattern))
        pattern = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.FullDateTimePattern;

    var start = pattern.IndexOf("h", StringComparison.InvariantCultureIgnoreCase);
    // cannot find the hour placeholder
    if (start < 0)
        return pattern;
    return pattern.Substring(0, start -1);
}

注意:
我仔细检查了所有的文化( CultureTypes.AllCultures ),而没有一个标准日期和时间模式以任何一个时间模式开始,因此这是用于标准日期和时间模式的安全逻辑。自定义模式是一个完整的其他故事。

NOTES: I double checked all the cultures (CultureTypes.AllCultures) and none of the standard date and time patterns start with a time pattern for any of them so this is safe logic to use for standard date and time patterns. Custom patterns are a whole other story.

FYI,任何文化的最长标准日期和时间模式是48,如果将其保存到数据库。

FYI, the longest standard date and time pattern for any culture is 48 in case you are saving this to a database.

推荐答案

如果框架自动提供了这样的话,我会感到惊讶。我似乎没有找到任何关于这一点。

I'd be surprised if the framework provided something like this automatically. I can't seem to find anything regarding this.

如果您向用户提供的日期时间格式列表在您的控制之下(即您不允许自由格式),一个选项是创建一个没有时间组件的正常格式字符串到一个查找表,如下所示:

If the list of datetime formats that you make available to users are under your control (i.e. you don't allow free-form formats), one option would be to create a lookup table from the normal format string to one without the time component, like so:

var mapping = new Dictionary<string, string>() {
    { "dddd, MMMM d, yyyy h:mm tt", "dddd, MMMM d, yyyy" },
    { "dddd, MMMM d, yyyy hh:mm tt", "dddd, MMMM d, yyyy" },
    ...
};

繁琐但非常简单,取决于您允许的格式数量。

Tedious, but extremely simple, depending on how many formats you allow.

或者,您可以选择创建一个简单的函数,尝试从格式字符串中删除时间组件。根据您的目标,这可能是一个有效的选择。日期时间格式字符串设计为文化适应性,如果不是严格正确。

Alternatively, you could opt to create a simple function which tries to remove the time component from the format string. Depending on your goal, this could be a valid option. Datetime format string are designed to be culturally adaptable, if not strictly correct.

或者,只要求您的用户也选择仅日期字符串的格式。

Alternatively, simply ask your user to also choose a format for date-only strings.

这篇关于从完整的DateTime模式中提取Date模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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