C#,日期时间格式,mont名称,cultureinfo [英] C#, datetime formatting, mont name, cultureinfo

查看:166
本文介绍了C#,日期时间格式,mont名称,cultureinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个小程序,我想根据cultureinfo清楚地显示日期和时间,如下所示: 3月3日,周日,上午7:46:57

I'm currently working on a small program which I want to show the date and time clearly and according to cultureinfo, like this: Sunday, march 3. 2017 7:46:57 AM

但是,我希望可以选择任何国家,并根据他们写日期的方式获得日期输出.以丹麦为例,我想展示一下:Søndag,3. marts 2017 7:46:57 (<-晚上是'19:46:57')

However, I want it to be possible to select any country, and get the date output according to their way of writing the date. In Denmark, as an example, I'd like to show: Søndag, 3. marts 2017 7:46:57 (<-- in the evening it would be '19:46:57')

请注意," 3 "在月份名称的另一侧.

Please note, that the '3' is on the other side of the month name.

我可以使用CultureInfo.GetCultures来获取所有cultureinfo的信息,没问题,但是当我显示日期时间时,我会使用MyDateTime.ToString(MyCultureInfo)获得"Sunday 3/12/2017 7:46:57 AM";

I can get all the cultureinfo's using CultureInfo.GetCultures, no problem, but when I show the datetime I get 'Sunday 3/12/2017 7:46:57 AM' using MyDateTime.ToString(MyCultureInfo);

我知道如何提取月份名称,并且我可以手动构造一个字符串,其中所有这些数据都可以显示出我所了解的文化所要的方式,但是我无法了解我所不知道的文化.如果有人选择中文"和阿拉伯语",我将不知道要显示什么.

I know how to extract the month name, and I could manually construct a string where all these data is showing the way I want for cultures I know, but I can not for cultures I do not know. If somebody chooses 'Chinese' og 'Arabic', I wouldn't know what to show.

我在想,是否有某种方法可以针对任何文化在正确的地方获取带有月份名称的完整日期?

I was thinking if there is some way to get the full date with month name in the right place for any culture?

推荐答案

我认为您要查找的格式不是所有区域性的默认格式.下面的代码将以默认的 Full 格式显示指定区域性的日期. F 用于 FullDateTimePattern .

I do not think the format you are looking for is the default format for all cultures. The code below will display the date in the default Full format for the specified culture. F is for FullDateTimePattern.

var now = DateTime.Now;
var denmark = now.ToString("F", new CultureInfo("da-DK"));

根据该行代码,输出为:

According to that line of code the output is:

12. marts 2017 03:34:12

因此,我的要点是:丹麦的默认完整日期时间模式如上所述.如果您想要其他格式,请使用下面的代码并根据需要进行调整,但请记住,丹麦人可能会发现这种模式很奇怪,因为他们可能不习惯这种模式:

So my takeaway from that is: The default full datetime pattern in Denmark is as the above. If you want another format, then use the code below and tweak it as you need to but keep in mind people in Denmark may find this pattern odd since they may not be used to it:

var denmarkCustomized = now.ToString("dddd, MMMM dd, yyyy h:mm:ss tt"
            , new CultureInfo("da-DK"));

这篇关于C#,日期时间格式,mont名称,cultureinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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