Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

2014-07-30

Strings to DateTime using C# (problems with different date formats from around the world)

// Date strings are interpreted according to the current culture. 
// If the culture is en-US, this is interpreted as "January 8, 2008",
// but if the user's computer is fr-FR, this is interpreted as "August 1, 2008" 
string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date);            
Console.WriteLine("Year: {0}, Month: {1}, Day: {2}", dt.Year, dt.Month, dt.Day);

// Specify exactly how to interpret the string.
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);

// Alternate choice: If the string has been input by an end user, you might  
// want to format it according to the current culture: 
// IFormatProvider culture = 
//     System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime dt2 = DateTime.Parse(date, culture, 
      System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine("Year: {0}, Month: {1}, Day {2}", 
      dt2.Year, dt2.Month, dt2.Day);

/* Output (assuming first culture is en-US and second is fr-FR):
    Year: 2008, Month: 1, Day: 8
    Year: 2008, Month: 8, Day 1
*/

I was encountering a lot of problems converting a simple string like
"30/7/2014 11:00" to DateTime because I kept getting an exception for invalid DateTime string.


I was doing the conversion using the code like

String datestr = "30/7/2014 11:00";
DateTime mydate = Convert.ToDateTime(datestr);

Finally, I dawned upon me that the source of the problem was that the machine I was
working on was set to "en-US" mm/dd/yyy and not the UK style dates dd/mm/yyyy which I normally worked with.

Without changing the machine setting, I had to use the CulturalInfo and DateTime.Parse instead. So the code ended up like the one below

String datestr = "30/7/2014 11:00";
IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);DateTime mydate = DateTime.Parse(datestr, culture);


The list of strings that can be used to set the CulturalInfo is below:
http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx


Note that for English we can have "en-US", "en-UK", "en-SG", "en-AU", "en-IN" 
Which leads me to wonder how many versions of English there are in the world.

The reverse path of formatting a DateTime to string is easier. For example, to get at date string in ISO format I can use
mydate.ToString("yyyy-MM-ddTHH:mm:sszz");

http://en.wikipedia.org/wiki/ISO_8601
provides a detailed description of the international standard for datetime string. This should work in all countries.

There is a way to parse the datetime string in a custom format that is culture independent. We should use DateTime.ParseExact method instead of DateTime.Parse


string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try 
{
    result = DateTime.ParseExact(dateString, format, provider);
    Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
    Console.WriteLine("{0} is not in the correct format.", dateString);
}







Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/