在.NET中获取当前的文化日名称 [英] Getting current culture day names in .NET

查看:160
本文介绍了在.NET中获取当前的文化日名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以从 DateTimeFormatInfo 获取 CurrentCulture 的工作日,但返回星期一<强>作为第一天,而不是星期日。而且,如果目前的文化不是英文(即ISO代码不是en),那么请默认为。

Is it possible to get the CurrentCulture's weekdays from DateTimeFormatInfo, but returning Monday as first day of the week instead of Sunday. And, if the current culture isn't English (i.e. the ISO code isn't "en") then leave it as default.

默认情况下 CultureInfo.CurrentCulture.DateTimeFormat.DayNames 返回:

[0]: "Sunday"
[1]: "Monday"
[2]: "Tuesday"
[3]: "Wednesday"
[4]: "Thursday"
[5]: "Friday"
[6]: "Saturday" 

但是我需要:


But I need:

[0]: "Monday"
[1]: "Tuesday"
[2]: "Wednesday"
[3]: "Thursday"
[4]: "Friday"
[5]: "Saturday" 
[6]: "Sunday"


推荐答案

您可以使用定制文化,以创建一个基于现有文化的新文化。说实话,我会说这可能有点沉重。 最简单的解决方案可能只是这样:

You can use custom cultures to create a new culture based off an existing one. To be honest, though, I'd say that's probably a bit heavy-handed. The "simplest" solution may just be something like:

public string[] GetDayNames()
{
    if (CultureInfo.CurrentCulture.Name.StartsWith("en-"))
    {
        return new [] { "Monday", "Tuesday", "Wednesday", "Thursday",
                        "Friday", "Saturday", "Sunday" };
    }
    else
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
    }
}

这篇关于在.NET中获取当前的文化日名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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