A common problem I find when dealing with dates is when parsing a string to save in Sql Server. By default, the DateTime.Parse() method assumes that dates given to it are in US format. In order to get it to process a non-US format date, such as the UK dd/mm/yyyy format, we need to pass it the appropriate “culture” information, like so:

// we need to have imported System.Globalization
using System.Globalization;

// fetch the en-GB culture
CultureInfo ukCulture = new CultureInfo(“en-GB”);
// pass the DateTimeFormat information to DateTime.Parse
DateTime myDateTime = DateTime.Parse(“18/09/2004”,ukCulture.DateTimeFormat);

Once converted, you can then call things like  myDateTime.ToShortDateString() and actually get the format you expect – nice!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s