如何显示具有选定日期部分的DateTime,但是按照FormatProvider的顺序显示? [英] How to display a DateTime with chosen date parts, but in the order of the FormatProvider?

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

问题描述



DateTime.Tostring()方法具有一个非常有用的模式列表,但我想要一个很小的变化在其中。



以下代码中使用的CultureInfo作为示例选择,我不想依赖于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 avril


我想要一种显示月份缩写的图案,这一天,但是保持来自指定CultureInfo的正确顺序。
使用模式MMM dd 将始终显示月份的缩写首先,然后显示当天,例如打破法国订单。


解决方案

显然,Microsoft接受格式如下的日期:

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

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

所以不要以为框架预览了你的情况。



您需要找到解决方法

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

用法:

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

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


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

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

The CultureInfo used in the following the following code are chosen as example, I don't want to rely on a specific list of CultureInfo, if possible

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

displays :

April 12
12 avril

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.

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

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

usage:

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

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

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

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