Showing posts with label DateTime format exception. Show all posts
Showing posts with label DateTime format exception. Show all posts

Monday, September 5, 2011

DateTime format problem in Csharp

Often we face this problem, there are may example on google, but still i feel like sharing this with you, this is how you can play with any date format in any culture.

DateTime dob = DateTime.Now.AddYears(-18);

 if (!string.IsNullOrEmpty(txtDOB.Text))
{
      IFormatProvider culture = new CultureInfo("en-US", true); // this is how you can set any culture you want !
  dob = DateTime.ParseExact(txtDOB.Text, "dd/MM/yyyy", culture);
 }

profile.DOB = dob;