如何显示与所选择的日期部分的日期时间,但在FormatProvider的顺序? [英] How to display a DateTime with chosen date parts, but in the order of the FormatProvider?

查看:141
本文介绍了如何显示与所选择的日期部分的日期时间,但在FormatProvider的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要显示在该培养提供顺序的时间,但与元件欲仅

I want to display the date in the order that the culture provides, but with the elements I want only.

本则DateTime.ToString()方法有模式是非常有用的一个列表但我想在一个非常小的变化。

The DateTime.Tostring() method has a list of patterns that are very useful but I would like a very small change in it.

CultureInfo的用于以下以下code被选作例子,我不想依靠的CultureInfo的具体名单,如果可能的话

var now = DateTime.Now;

string nowString = now.ToString("m", CultureInfo.GetCultureInfo("en-us"));
Console.WriteLine(nowString);

nowString = now.ToString("m", CultureInfo.GetCultureInfo("fr-FR"));
Console.WriteLine(nowString);

显示:

4月12日
  12艾薇儿

April 12
12 avril

我想中显示的月份和一天的缩写的图案,但,保持从指定的CultureInfo正确的顺序。 使用模式的MMM DD 将始终显示在一个月的缩写第一,其次是天,打破了法国的订单为例。

I would like a pattern that display the abbreviation of the month and the day, but that keeps the correct order from the specified CultureInfo. using the pattern "MMM dd" will always display the month's abbreviation first, followed by the day, breaking the french order for example.

任何的方式来实现,没有太多的定制code?

Any way to achieve that without too much custom code?

推荐答案

显然,微软的接受日期要设置格式是这样的:

apparently, Microsoft "accepts" the date to be formatted like this:

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays ven. 29 août

所以,不要以为框架previewed东西给你的情况。

So don't think that Framework previewed something for your case.

您将需要找到一个解决方法是这样的:

You will need to find a workaround like this:

private string GetCultureMonthDay(CultureInfo culture, DateTime date)
{
    return string.Format(culture, "{0:" + 
      culture.DateTimeFormat.MonthDayPattern.Replace("MMMM", "MMM") + "}", date);
}

用法:

?Console.WriteLine(GetCultureMonthDay(CultureInfo.GetCultureInfo("fr-FR"), now));
12 avr.

?Console.WriteLine(GetCultureMonthDay(CultureInfo.GetCultureInfo("en-US"), now));
Apr 12

这篇关于如何显示与所选择的日期部分的日期时间,但在FormatProvider的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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