Last active
March 21, 2019 13:15
-
-
Save morales-franco/a51fb213a9b65b2a93459957cc4473e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES"); | |
string valueToConvert = "24/12/2020 23:50"; | |
Console.WriteLine($"(1) Change Current Culture to { Thread.CurrentThread.CurrentCulture.Name }"); | |
Console.WriteLine($"(1) Try to convert { valueToConvert } using { Thread.CurrentThread.CurrentCulture.Name } culture - Default Culture Format [dd/MM/yyyy]..."); | |
var parseValue = Convert.ToDateTime(valueToConvert); | |
Console.WriteLine("-->Success!"); | |
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); | |
try | |
{ | |
Console.WriteLine($"(2) Change Current Culture to { Thread.CurrentThread.CurrentCulture.Name }"); | |
Console.WriteLine($"(2) Try to convert { valueToConvert } using { Thread.CurrentThread.CurrentCulture.Name } culture - Default Culture Format [MM/dd/yyyy]..."); | |
parseValue = Convert.ToDateTime(valueToConvert); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"-->Failed! Convert { ex.Message }"); | |
} | |
Console.WriteLine($"(3) Current Culture { Thread.CurrentThread.CurrentCulture.Name }"); | |
parseValue = Convert.ToDateTime(valueToConvert, CultureInfo.CreateSpecificCulture("es-ES")); | |
Console.WriteLine($"(3) Try to convert { valueToConvert } using a SPECIFIC CULTURE es-ES - Default Culture Format [dd/MM/yyyy]..."); | |
Console.WriteLine("-->Success!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dateNow = DateTime.Now; | |
dateNow.ToString(CultureInfo.CreateSpecificCulture("fr-FR")); | |
dateNow.ToString(CultureInfo.CreateSpecificCulture("es-AR")); | |
dateNow.ToString(CultureInfo.CreateSpecificCulture("en-US")); | |
dateNow.ToString(CultureInfo.CreateSpecificCulture("zh-CN")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dateNow = DateTime.Now; | |
Console.WriteLine($"Current Culture: { Thread.CurrentThread.CurrentCulture.Name }"); | |
Console.WriteLine($"Date Now - : { dateNow.ToString() }"); | |
//Change current culture to en-US | |
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); | |
Console.WriteLine($"Current Culture: { Thread.CurrentThread.CurrentCulture.Name }"); | |
Console.WriteLine($"Date Now - : { dateNow.ToString() }"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string value = "20/11/2019 22:09"; | |
DateTime result; | |
//(1)- Success | |
Console.WriteLine($"Try to parse { value } to { Thread.CurrentThread.CurrentCulture.Name } culture..."); | |
if (!DateTime.TryParse(value, out result)) | |
Console.WriteLine($"Parse Error to { Thread.CurrentThread.CurrentCulture.Name } culture!"); | |
else | |
Console.WriteLine("Success!"); | |
//(2)-Failed! | |
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); | |
Console.WriteLine($"Try to parse { value } to { Thread.CurrentThread.CurrentCulture.Name } culture..."); | |
if (!DateTime.TryParse(value, out result)) | |
Console.WriteLine($"Parse Error to { Thread.CurrentThread.CurrentCulture.Name } culture!"); | |
else | |
Console.WriteLine("Success!"); | |
//(3)-Failed! | |
Console.WriteLine($"Try to parse Exact { value } - Format: dd/MM/yyyy HH:mm:ss..."); | |
if (!DateTime.TryParseExact(value, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result)) | |
Console.WriteLine($"Try Parse Exact Error - Format: dd/MM/yyyy HH:mm:ss"); | |
else | |
Console.WriteLine("Success!"); | |
//(4)-Success! | |
Console.WriteLine($"Try to parse Exact { value } - Format: dd/MM/yyyy HH:mm..."); | |
if (!DateTime.TryParseExact(value, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result)) | |
Console.WriteLine($"Try Parse Exact Error - Format: dd/MM/yyyy HH:mm"); | |
else | |
Console.WriteLine("Success!"); | |
//(5)-Success! | |
Console.WriteLine($"Try to parse Exact { value } - Many formats..."); | |
string[] format = new string[] { "yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm", "dd/MM/yyyy HH:mm:ss", "MM/dd/yyyy HH:mm", "dd/MM/yyyy", "MM/dd/yyyy" }; | |
if (!DateTime.TryParseExact(value, format, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result)) | |
Console.WriteLine($"Try Parse Exact Error to { Thread.CurrentThread.CurrentCulture.Name } culture!"); | |
else | |
Console.WriteLine("Success!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment