DateTime.Parse()

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!

reference: http://www.developerfusion.co.uk/show/4690/

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *