Convert Date to appropriate format

R

RP

I am using SQL Server 2005 as backend. I have a Text Box that accepts
Date in the format dd-MM-yyyy. But when I attempt to insert a record,
an error is displayed: Cannot convert char to DateTime ....

Actually, when the text box has entry like 22-10-2005, it considers 22
as month whereas it is DD. How to convert the Text Box value to an
acceptable format.

I tried: DateTime.parse(txtDOB.Text,"dd-MM-yyyy")

It gives error.
 
T

Tom Porterfield

RP said:
I am using SQL Server 2005 as backend. I have a Text Box that accepts
Date in the format dd-MM-yyyy. But when I attempt to insert a record,
an error is displayed: Cannot convert char to DateTime ....

Actually, when the text box has entry like 22-10-2005, it considers 22
as month whereas it is DD. How to convert the Text Box value to an
acceptable format.

I tried: DateTime.parse(txtDOB.Text,"dd-MM-yyyy")

It gives error.

There is no overload of DateTime.Parse that takes a date and a format.
Use ParseExact instead. Ex:

DateTime dt = DateTime.ParseExact(txtDOB.text, "dd-MM-yyyy",
System.Globalization.DateTimeFormatInfo.CurrentInfo);
 
J

joachim

what's wrong with?

string inputSplit = txtDOB.Text.Split(new char[] { '-' });
// dd-MM-yyyy to yyyy-MM-dd
DateTime newDate = new DateTime(
Convert.ToInt16(inputSplit[2]),
Convert.ToInt16(inputSplit[1]),
Convert.ToInt16(inputSplit[0]));

Regards,
Joachim
 
R

Rad [Visual C# MVP]

I am using SQL Server 2005 as backend. I have a Text Box that accepts
Date in the format dd-MM-yyyy. But when I attempt to insert a record,
an error is displayed: Cannot convert char to DateTime ....

Actually, when the text box has entry like 22-10-2005, it considers 22
as month whereas it is DD. How to convert the Text Box value to an
acceptable format.

I tried: DateTime.parse(txtDOB.Text,"dd-MM-yyyy")

It gives error.

Hi RP,

You could eliminate 99% of your headaches by using more appropriate
controls like a datepicker or a calendar. The reason being you do not
need to write any validation code and your date serialization will be
independent of gotchas like client pc's regional settings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top