C#时间之不同国家的显示格式

对于时间的显示,不同的地方有不同的时间格式,代码如下:

public class Common_DateFormat
{
    public Common_DateFormat()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    /***********************************************/
    //常用的cultureName只列表,变量cultureName的值为区域性名称,如zh-CN
    //区域性名称    语言-国家/地区
    //zh-CN         中文 - 中国
    //zh-TW         中文 - *
    //en-US         英语 - 美国
    //en-GB         英语 - 英国
    //ja-JP         日语 - 日本
    //zh-SG         中文 - 新加坡  
    //ru-RU         俄语 - 俄罗斯  用.分割日期
    /***********************************************/
    //如果cultureName为空,则取当前系统的Culture的值。

    //格式:2009-8-5 :5:05:01
    public static string DisplayDateByCultureName(DateTime dtDate,int nFormat)
    {
        // Create the CultureInfo object for the specified culture,
        // and use it as the IFormatProvider when converting the date.
        string cultureName = ConfigurationManager.AppSettings.Get("CultureName");

        if (string.IsNullOrEmpty(cultureName)) //如果为空,则取当前系统的文化值

        {
            cultureName = System.Globalization.CultureInfo.CurrentCulture.ToString();        
        }
        IFormatProvider culture = new System.Globalization.CultureInfo(cultureName, true);
        string dateString = string.Empty;
        if (cultureName.ToLower()=="zh-cn")//中国-中文
        {
            switch (nFormat)
            {
                case 1:
                    //dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    dateString = string.Format(culture, "{0:yyyy-MM-dd}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    break;
                case 2:
                    //dateString = string.Format(culture, "{0:g}", dtDate);//2016-11-5 14:23
                    dateString = string.Format(culture, "{0:yyyy-MM-dd HH:mm}", dtDate);//2016-11-5 14:23
                    break;
                case 3:
                    //dateString = string.Format(culture, "{0:G}", dtDate);//2016-11-5 14:23:23
                    dateString = string.Format(culture, "{0:yyyy-MM-dd hh:mm:ss}", dtDate);//2016-11-5 14:23:23
                    break;
                case 4:
                    dateString = string.Format(culture, "{0:t}", dtDate);//14:23
                    break;
                case 5:
                    dateString = string.Format(culture, "{0:T}", dtDate);//14:23:23
                    break;
                case 6:
                    dateString = string.Format(culture, "{0:D}", dtDate);//2016年11月5日

                    break;
                case 7:
                    dateString = string.Format(culture, "{0:f}", dtDate);//2016年11月5日 14:23
                    break;
                case 8:
                    dateString = string.Format(culture, "{0:F}", dtDate);//2016年11月5日 14:23:23
                    break;
                case 9:
                    dateString = string.Format(culture, "{0:Y}", dtDate);//2016年11月

                    break;
                case 10:
                    dateString = string.Format(culture, "{0:M}", dtDate);//11月5日

                    break;
                case 11:
                    dateString = string.Format(culture, "{0:R}", dtDate);//Sat, 05 Nov 2016 14:23:23 GMT
                    break;
                case 12:
                    dateString = string.Format(culture, "{0:s}", dtDate);//2016-11-05T14:23:23
                    break;
                case 13:
                    dateString = string.Format(culture, "{0:u}", dtDate);//2016-11-05 14:23:23Z
                    break;
                case 14:
                    dateString = string.Format(culture, "{0:U}", dtDate);//2016年11月5日 6:23:23
                    break;
                case 15:
                    dateString = string.Format(culture, "{0}", dtDate);  //2016-11-5 14:23:23
                    break;
                case 16:
                    dateString = string.Format(culture, "{0:yyyyMMddHHmmssffff}", dtDate);
                    break;
                case 17:
                    dateString = string.Format(culture, "{0:yyyy-MMM-dd}", dtDate); //5-Nov-2016
                    break;
                default:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5
                    break;
            }
        }
        else if (cultureName.ToLower()=="en-us")//英语-美国
        {
            switch (nFormat)
            {
                case 1:
                    dateString = string.Format(culture, "{0:MM/dd/yyyy}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    break;
                case 2:
                    dateString = string.Format(culture, "{0:MM/dd/yyyy hh:mm}", dtDate);//2016-11-5 14:23
                    break;
                case 3:
                    dateString = string.Format(culture, "{0:MM/dd/yyyy hh:mm:ss}", dtDate);//2016-11-5 14:23:23
                    break;
                case 4:
                    dateString = string.Format(culture, "{0:t}", dtDate);//14:23
                    break;
                case 5:
                    dateString = string.Format(culture, "{0:T}", dtDate);//14:23:23
                    break;
                case 6:
                    dateString = string.Format(culture, "{0:D}", dtDate);//2016年11月5日

                    break;
                case 7:
                    dateString = string.Format(culture, "{0:f}", dtDate);//2016年11月5日 14:23
                    break;
                case 8:
                    dateString = string.Format(culture, "{0:F}", dtDate);//2016年11月5日 14:23:23
                    break;
                case 9:
                    dateString = string.Format(culture, "{0:Y}", dtDate);//2016年11月

                    break;
                case 10:
                    dateString = string.Format(culture, "{0:M}", dtDate);//11月5日

                    break;
                case 11:
                    dateString = string.Format(culture, "{0:R}", dtDate);//Sat, 05 Nov 2016 14:23:23 GMT
                    break;
                case 12:
                    dateString = string.Format(culture, "{0:s}", dtDate);//2016-11-05T14:23:23
                    break;
                case 13:
                    dateString = string.Format(culture, "{0:u}", dtDate);//2016-11-05 14:23:23Z
                    break;
                case 14:
                    dateString = string.Format(culture, "{0:U}", dtDate);//2016年11月5日 6:23:23
                    break;
                case 15:
                    dateString = string.Format(culture, "{0}", dtDate);  //2016-11-5 14:23:23
                    break;
                case 16:
                    dateString = string.Format(culture, "{0:yyyyMMddHHmmssffff}", dtDate);
                    break;
                case 17:
                    dateString = string.Format(culture, "{0:MMM dd yyyy}", dtDate); //5-Nov-2016
                    break;
                default:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5
                    break;
            }
        }
        else if (cultureName.ToLower()=="en-gb")//英语-英国
        {
            switch (nFormat)
            {
                case 1:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    break;
                case 2:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy hh:mm}", dtDate);//2016-11-5 14:23
                    break;
                case 3:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy hh:mm:ss}", dtDate);//2016-11-5 14:23:23
                    break;
                case 4:
                    dateString = string.Format(culture, "{0:t}", dtDate);//14:23
                    break;
                case 5:
                    dateString = string.Format(culture, "{0:T}", dtDate);//14:23:23
                    break;
                case 6:
                    dateString = string.Format(culture, "{0:D}", dtDate);//2016年11月5日

                    break;
                case 7:
                    dateString = string.Format(culture, "{0:f}", dtDate);//2016年11月5日 14:23
                    break;
                case 8:
                    dateString = string.Format(culture, "{0:F}", dtDate);//2016年11月5日 14:23:23
                    break;
                case 9:
                    dateString = string.Format(culture, "{0:Y}", dtDate);//2016年11月

                    break;
                case 10:
                    dateString = string.Format(culture, "{0:M}", dtDate);//11月5日

                    break;
                case 11:
                    dateString = string.Format(culture, "{0:R}", dtDate);//Sat, 05 Nov 2016 14:23:23 GMT
                    break;
                case 12:
                    dateString = string.Format(culture, "{0:s}", dtDate);//2016-11-05T14:23:23
                    break;
                case 13:
                    dateString = string.Format(culture, "{0:u}", dtDate);//2016-11-05 14:23:23Z
                    break;
                case 14:
                    dateString = string.Format(culture, "{0:U}", dtDate);//2016年11月5日 6:23:23
                    break;
                case 15:
                    dateString = string.Format(culture, "{0}", dtDate);  //2016-11-5 14:23:23
                    break;
                case 16:
                    dateString = string.Format(culture, "{0:yyyyMMddHHmmssffff}", dtDate);
                    break;
                case 17:
                    dateString = string.Format(culture, "{0:dd MMM yyyy}", dtDate); //5-Nov-2016
                    break;
                default:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5
                    break;
            }
        }
        else if (cultureName.ToLower() == "zh-sg")//中文-新加坡

        {
            switch (nFormat)
            {
                case 1:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    break;
                case 2:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy hh:mm}", dtDate);//2016-11-5 14:23
                    break;
                case 3:
                    dateString = string.Format(culture, "{0:dd/MM/yyyy hh:mm:ss}", dtDate);//2016-11-5 14:23:23
                    break;
                case 4:
                    dateString = string.Format(culture, "{0:t}", dtDate);//14:23
                    break;
                case 5:
                    dateString = string.Format(culture, "{0:T}", dtDate);//14:23:23
                    break;
                case 6:
                    dateString = string.Format(culture, "{0:D}", dtDate);//2016年11月5日

                    break;
                case 7:
                    dateString = string.Format(culture, "{0:f}", dtDate);//2016年11月5日 14:23
                    break;
                case 8:
                    dateString = string.Format(culture, "{0:F}", dtDate);//2016年11月5日 14:23:23
                    break;
                case 9:
                    dateString = string.Format(culture, "{0:Y}", dtDate);//2016年11月

                    break;
                case 10:
                    dateString = string.Format(culture, "{0:M}", dtDate);//11月5日

                    break;
                case 11:
                    dateString = string.Format(culture, "{0:R}", dtDate);//Sat, 05 Nov 2016 14:23:23 GMT
                    break;
                case 12:
                    dateString = string.Format(culture, "{0:s}", dtDate);//2016-11-05T14:23:23
                    break;
                case 13:
                    dateString = string.Format(culture, "{0:u}", dtDate);//2016-11-05 14:23:23Z
                    break;
                case 14:
                    dateString = string.Format(culture, "{0:U}", dtDate);//2016年11月5日 6:23:23
                    break;
                case 15:
                    dateString = string.Format(culture, "{0}", dtDate);  //2016-11-5 14:23:23
                    break;
                case 16:
                    dateString = string.Format(culture, "{0:yyyyMMddHHmmssffff}", dtDate);
                    break;
                case 17:
                    dateString = string.Format(culture, "{0:dd-MMM-yyyy}", dtDate); //5-Nov-2016
                    break;
                default:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5
                    break;
            }
        }
        else
        {
            switch (nFormat)
            {
                case 1:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5  注释以中文-简体为例说明

                    break;
                case 2:
                    dateString = string.Format(culture, "{0:g}", dtDate);//2016-11-5 14:23
                    break;
                case 3:
                    dateString = string.Format(culture, "{0:G}", dtDate);//2016-11-5 14:23:23
                    break;
                case 4:
                    dateString = string.Format(culture, "{0:t}", dtDate);//14:23
                    break;
                case 5:
                    dateString = string.Format(culture, "{0:T}", dtDate);//14:23:23
                    break;
                case 6:
                    dateString = string.Format(culture, "{0:D}", dtDate);//2016年11月5日

                    break;
                case 7:
                    dateString = string.Format(culture, "{0:f}", dtDate);//2016年11月5日 14:23
                    break;
                case 8:
                    dateString = string.Format(culture, "{0:F}", dtDate);//2016年11月5日 14:23:23
                    break;
                case 9:
                    dateString = string.Format(culture, "{0:Y}", dtDate);//2016年11月

                    break;
                case 10:
                    dateString = string.Format(culture, "{0:M}", dtDate);//11月5日

                    break;
                case 11:
                    dateString = string.Format(culture, "{0:R}", dtDate);//Sat, 05 Nov 2016 14:23:23 GMT
                    break;
                case 12:
                    dateString = string.Format(culture, "{0:s}", dtDate);//2016-11-05T14:23:23
                    break;
                case 13:
                    dateString = string.Format(culture, "{0:u}", dtDate);//2016-11-05 14:23:23Z
                    break;
                case 14:
                    dateString = string.Format(culture, "{0:U}", dtDate);//2016年11月5日 6:23:23
                    break;
                case 15:
                    dateString = string.Format(culture, "{0}", dtDate);  //2016-11-5 14:23:23
                    break;
                case 16:
                    dateString = string.Format(culture, "{0:yyyyMMddHHmmssffff}", dtDate);
                    break;
                case 17:
                    dateString = string.Format(culture, "{0:yyyy MMM dd}", dtDate); //5-Nov-2016
                    break;
                default:
                    dateString = string.Format(culture, "{0:d}", dtDate);//2016-11-5
                    break;
            }
        
        }

        return dateString;
    }`

C#时间之不同国家的显示格式

上一篇:C# MVC 实现登录的5种方式


下一篇:WPF入门教程系列四——Dispatcher介绍