如果区域OS为泰语,

时间:2018-07-12 14:50:19

标签: c# datetime

我有一个要从Excel中提取的日期字符串,我正在尝试解析。所述日期最初是公历格式。即使在使用TryParse / TryParseExact之后,还有什么方法可以保留呢?

注意:由于我们的最终用户来自泰国,因此我将机器的区域设置为泰国时就这样做了。因此,截至本文发布之日,我的系统日期为"12/7/2561"

假设我输入了以下格里高利语M/d/yyyy格式的字符串:

string validDate = "7/15/2018"

当我最初尝试使用DateTime.TryParseExact进行解析时,它给了我15/7/2561

我尝试了这些链接中列出的以下方法,尤其是CreateSpecificCulture

  1. MSDN: Convert Thailand DateTime TO English DateTime
  2. ASP.NET forums: DateTime issue while converting from thai culture to english culture

这是我的代码:

public DateTime? ConvertThaiToGregorian(string text, string dateFormat)
{
    /// Note: Thailand uses the Thai Buddhist calendar, which is 543 years ahead of 
    ///       the Gregorian calendar. 
    /// dateFormat here is "M/d/yyyy".

    DateTime convertedDate;

    // Create Thai CultureInfo
    IFormatProvider thCulture = CultureInfo.CreateSpecificCulture("th-TH");

    // Parse date = returns "15/7/2018"...
    DateTime.TryParseExact(text, dateFormat, thCulture, DateTimeStyles.None, out convertedDate);

    // Create en-US CultureInfo
    IFormatProvider engCulture = CultureInfo.CreateSpecificCulture("en-US");

    // Parse date back to US format = gives me "01/01/0544"...
    DateTime.TryParseExact(convertedDate.ToString(), dateFormat, engCulture, DateTimeStyles.None, out convertedDate);

    return convertedDate;
}

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

希望它可以帮助您了解CultureInfo如何从字符串中获取DateTime,最起码的要求是哪种文化日期字符串是正确的,但是服务器或操作系统对字符串的解析是否正确无关紧要。仅当解析字符串时未指定区域性时,操作系统才会干扰。

C#在这里拨弄:https://dotnetfiddle.net/wdDlts

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string thaiBudistDate = "12/7/2561";

        // US format
        CultureInfo provider = CultureInfo.GetCultureInfo("en-US");
        DateTime date = DateTime.Parse(thaiBudistDate, provider);
        Console.WriteLine("Original string: '" + provider + "' in 'en-US' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );

        // Thai Culture
        provider = CultureInfo.GetCultureInfo("th-TH");
        date = DateTime.Parse(thaiBudistDate, provider);
        Console.WriteLine("Original string: '" + provider + "' in 'th-TH' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );

        // Thai Culture format! M/d/yyyy
        provider = CultureInfo.GetCultureInfo("th-TH");
        var format = "M/d/yyyy";
        date = DateTime.ParseExact(thaiBudistDate, format, provider);
        Console.WriteLine("Original string: '" + provider + "' in 'th-TH' format specified 'M/d/yyyy' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );

        // Thai Culture format d/M/yyyy 
        provider = CultureInfo.GetCultureInfo("th-TH");
        format = "d/M/yyyy";
        date = DateTime.ParseExact(thaiBudistDate, format, provider);
        Console.WriteLine("Original string: '" + provider + "' in 'th-TH' format specified 'd/M/yyyy' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );

        // Using Gregorian time
        string usaDate = "12/7/2018";
        // US Culture
        provider = CultureInfo.GetCultureInfo("en-US");
        date = DateTime.Parse(usaDate, provider);
        Console.WriteLine("Original string: '" + usaDate + "' in 'en-Us' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );


        // You got the point, dones't matter what provider you use! Hope this will help you undestand how wondows Culture works
        usaDate = "12/7/2018";
        // Thai Culture
        provider = CultureInfo.GetCultureInfo("th-TH");
        date = DateTime.Parse(usaDate, provider);
        Console.WriteLine("Original string: '" + usaDate + "' in 'th-TH' => Day: " + date.Day + " Month: " + date.Month + " Year: " + date.Year );

    }
}

答案 1 :(得分:0)

如果您已经将日期时间转换为泰国文化(正确设置了日,月和年属性),我认为它足以将其再次格式化为Datetime.ToString(“ M / d / yyyy”),以备不时之需字符串表示形式。