Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
String was not recognized as a valid DateTime error shows when i am trying to convert date. By default my date is in
Fri Apr 03 10:12:00 IST 2015

format.I want to convert it in "MM/dd/yyyy" i.e in en-US short date.

CurrentStartTransactionManagement.Text = pTask.StartDate.ToString();
In the above line of code i am trying to convert the date and displaying it in the textbox. But when i do that the date is displayed in this
Fri Apr 03 10:12:00 IST 2015
format. But actually i want to display date in 04/03/2015 format.
Posted
Comments
[no name] 20-Nov-15 2:41am    
your date should be like this "Fri Apr 03 2015 10:12:00".

You need to use an overload of ToString() on DateTime[^] that takes format string and format provider:
C#
pTask.StartDate.ToString("d", new System.Globalization.CultureInfo("en-US"))
 
Share this answer
 
v3
Try-
C#
CurrentStartTransactionManagement.Text = pTask.StartDate.ToString("MM/dd/yyyy");
 
Share this answer
 
C#
System.Globalization.CultureInfo cultureinfo =        new System.Globalization.CultureInfo("en-US");
   CurrentStartTransactionManagement.Text = pTask.StartDate.ToString();
   DateTime dt = DateTime.Parse( CurrentStartTransactionManagement.Text , cultureinfo);



C#
DateTime dof = new DateTime();

                       System.Globalization.CultureInfo c1 = new System.Globalization.CultureInfo("en-US", true);
                       dof = DateTime.Parse(CurrentStartTransactionManagement.Text.Trim(), c1, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900