Error while converting into DateTime

G

Guest

Hi,

I am having a DataTable which one column is in string format now i want to
change that one to DataTime. I don't know whether its in datetime format or
not i have to check if it is in DateTime Format some times it may in normal
string not a DateTime. If it is in any one of the DateTime format then i have
to change it as a Specfic format as "21-03-2005 11:34" using C#.Net.

If its not in DateTime format then i should give a message.

txt3.Text = txt3.Text.Replace("AM","");
txt3.Text = txt3.Text.Replace("PM","");
string strDateTime = txt3.Text.Trim();
DateTime dtConvert = DateTime.Parse(strDateTime);

// Format to 14-05-2006
string strConvert = dtConvert.ToString("dd-MM-yyyy");

I used this type but i am getting error as "String is not in a correct format"

I don't know what to do. any one please help.
 
J

Jianwei Sun

Look into the DateTime.TryParse(), and it won't throw an exception, and
let you know if the string is in the right format or not.
 
G

Guest

DOH!

I'm really sorry, I had forgotten I had changed newsgroups.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 
M

Mythran

Mythran said:
If this is C# 2005, isn't there a TryParse method on DateTime?

HTH,
Mythran

Woops, didn't see Jianwei's reply which says use TryParse :)

Mythran
 

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

Top