Skip to content

Instantly share code, notes, and snippets.

@morales-franco
Last active March 21, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morales-franco/a51fb213a9b65b2a93459957cc4473e0 to your computer and use it in GitHub Desktop.
Save morales-franco/a51fb213a9b65b2a93459957cc4473e0 to your computer and use it in GitHub Desktop.
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!");
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"));
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() }");
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