如何将CurrentCulture格式的日期时间转换为Gregorian DateTime?

时间:2013-04-12 05:59:54

标签: c# datetime

我将Global.asax设为:

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        PersianCulture fa = new PersianCulture();

        Thread.CurrentThread.CurrentCulture = fa;
    }

我想将“1392 1 23”转换为“2013 4 12”。

我该怎么做?

3 个答案:

答案 0 :(得分:9)

没有“波斯语DateTime”这样的东西。 DateTime始终是公历日历值,没有特定格式。当您对其进行格式化时(通常通过调用ToString),您可以确定其格式化方式 - 如果您使用的文化使用非公历日历,则会将原始值转换为该日历。

例如,如果您想解析波斯语用户的输入,然后将其转换为英语用户可以理解的等效日期,您可以使用:

DateTime date = DateTime.Parse(text, persianCulture);
string englishText = date.ToString(englishCulture);

答案 1 :(得分:2)

我不相信英语日期时间

来自MSDN;

  

DateTime值类型表示值范围的日期和时间   从00:00:00(午夜),1月1日,0001 Anno Domini(Common Era)   截止时间:9:59:59,9999年12月31日A.D.(C.E。)在 Gregorian   日历。

当您Culture使用DateTime时,它会自动转换为此日历。

  

CultureInfo类提供特定于文化的信息,例如   语言,子语言,国家/地区,日历和惯例   与特定文化相关联。该课程还提供访问权限   特定于文化的DateTimeFormatInfo实例,   NumberFormatInfoCompareInfoTextInfo个对象。这些对象   包含特定文化操作所需的信息,等   作为大小写,格式化日期和数字,以及比较字符串。

答案 2 :(得分:1)

您可以使用DateTime.Parse()

 System.Globalization.CultureInfo cultureinfo = 
    new System.Globalization.CultureInfo("en-US");
    DateTime dt = DateTime.Parse("11/04/2013", cultureinfo);