Sunday 13 July 2014

Convert.ToDateTime

Error: not able to convert string to DateTime
How to Convert dd/MM/yyyy to MM/dd/yyyy
How to Convert dd/MMM/yyyy to MM/dd/yyyy

How to convert textbox value date from dd/MM/yyyy to MM/dd/yyyy
Normally we will face some problem with datetime control in any application because of local datetime formats. It will differ for each user how they have configured in the local computer. Now i am giving a generic solution for any type of datetime formats in the local system.
string date=System.DateTime.Now.ToString();
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
Response.Write(Convert.ToDateTime(date));