Reputation: 365
I have a date string pulled from Excel which I'm trying to parse. Said date is originally in Gregorian format. Is there any way to retain this even after using TryParse
/TryParseExact
?
Note: Since our end-users are from Thailand, I'm doing this while my machine's region is set to Thai. Hence, my system date as of this post is "12/7/2561"
.
Suppose I have the following string input in Gregorian M/d/yyyy
format:
string validDate = "7/15/2018"
When I initially tried parsing this using DateTime.TryParseExact
, it gave me 15/7/2561
.
I've tried the following methods listed in these links, particularly the CreateSpecificCulture
:
Here's my code right now:
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;
}
Any help will be greatly appreciated. Thank you!
Upvotes: 0
Views: 3710
Reputation: 4902
Hope it will help you understand how CultureInfo gets the DateTime from a string, the bare minimum requirement is in what culture date string is, but server or operating system shouldn't matter if string is parsed correct. Operating system will interfere only if culture isn't specified when string is parsed.
C# fiddle here: 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 );
}
}
Upvotes: 1
Reputation: 11
If you already have the datetime converted to Thai culture (where day, month and year properties are correctly set) I think its enough to format it again to Datetime.ToString("M/d/yyyy") in case you want the string representation.
Upvotes: 0