显示等效项为"MMM d,yyyy".在当前的文化信息中 [英] Display equivalent of "MMM d, yyyy" in current culture info

查看:168
本文介绍了显示等效项为"MMM d,yyyy".在当前的文化信息中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我细读了C#提供的标准日期时间格式化程序,但似乎找不到符合我的短日期"要求的格式程序:"2013年5月4日".我知道我可以做一个自定义格式的字符串来实现目标:

I've perused the standard datetime formatters that C# offers, and I can't seem to find one that matches my "short date" requirement: "May 4, 2013". I know that I can do a custom format string to accomplish the goal:

myDateTimeOffset.ToString("MMM d, yyyy");

但是,此应用程序可能会转到其他国家/地区.有没有办法使当前区域性等效于特定格式的字符串?

However, this application may go to other countries. Is there a way to get the current culture's equivalent to a particular format string?

myDateTimeOffset.ToString("MMM d, yyyy", CultureInfo.CurrentUICulture);

它是否适应外国文化中的等效短日期"格式?

And have it adapt to an equivalent "short date" format in the foreign culture?

推荐答案

我认为没有默认格式选项可以满足您的特定要求(D可能会接近).我在书签中拥有此概述已有10多年了,并且发现它仍然有用: http://samples. pdmlab.com/

I don't think there's any default format option baked in that maches your specific requirement (D may come close). I've this overview in my bookmarks for more than 10 years now, and I find it still useful: http://samples.pdmlab.com/

无论如何,由于您的应用程序可能无法支持多种语言,因此,我认为您的最佳选择是根据需要使用的实际语言,使用某种DateTime扩展方法和格式,甚至必要的话.

Anyway, as your app may not be available for an infinite amount of languages, I think your best choice will be to use some kind of DateTime extension method and format depending on the actual language being used, if even necessary.

这篇关于显示等效项为"MMM d,yyyy".在当前的文化信息中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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