Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Convert.ToDateTime(txtdob.Text,"dd/mm/yyyy")
i get an exception of this

String was not recognized as a valid DateTime.

plz help me
Posted

Hi,

you can make use of DateTime.TryParse function.

refer DateTime.TryParse Method [^]

hope it helps.
 
Share this answer
 
C#
var date=DateTime.MinValue;
var dateStr=string.Empty;
if(DateTime.TryParse(txtMyDate.Text, out date))
{
  dateStr=date.ToString("dd/MM/yyyy");
}


But as ratnakargudipati said, it is dependent on System Date time Format.
 
Share this answer
 
If your textbox takes date in dd/MM/yyyy format, the following code will convert it into valid datetime value:
C#
string InputDate = YourDateTextBoxId.Text.Trim();
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime OutputDate = Convert.ToDateTime(InputDate, dateInfo); 
 
Share this answer
 
Try this:-

DateTime date = DateTime.ParseExact(txtdob.Text, "dd/MM/yyyy", null);
 
Share this answer
 
try these,

C#
txtbox.Text = "2/30/1990";
dateTime dt=txtbox.toShortDateString();
 
Share this answer
 
To convert DateTime format check your System datetime format. It will convert to your system datetime format
 
Share this answer
 
try this...
C#
txtbox.Text = "12/30/1990"
string var=Convert.ToDateTime(txtbox.Text).ToString("dd/MM/yyyy");
 
Share this answer
 

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