By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

Error while converting into DateTime

P: n/a
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.
Aug 9 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Have you tried using the IsDate function? By using that you can determine if
the string you are looking at can be converted to DateTime. Here is a link:

http://msdn2.microsoft.com/en-us/lib...3t(d=ide).aspx

HTH

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
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.
Aug 9 '06 #2

P: n/a
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.

B.N.Prabhu wrote:
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.
Aug 9 '06 #3

P: n/a
I am sorry friend, I want in C#.net not in VB.Net

"WhiteWizard" wrote:
Have you tried using the IsDate function? By using that you can determine if
the string you are looking at can be converted to DateTime. Here is a link:

http://msdn2.microsoft.com/en-us/lib...3t(d=ide).aspx

HTH

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
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.
Aug 9 '06 #4

P: n/a
DOH!

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

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
I am sorry friend, I want in C#.net not in VB.Net

"WhiteWizard" wrote:
Have you tried using the IsDate function? By using that you can determine if
the string you are looking at can be converted to DateTime. Here is a link:

http://msdn2.microsoft.com/en-us/lib...3t(d=ide).aspx

HTH

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
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.
Aug 9 '06 #5

P: n/a

"WhiteWizard" <Wh*********@discussions.microsoft.comwrote in message
news:5E**********************************@microsof t.com...
Have you tried using the IsDate function? By using that you can determine
if
the string you are looking at can be converted to DateTime. Here is a
link:

http://msdn2.microsoft.com/en-us/lib...3t(d=ide).aspx

HTH

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
>Hi,
If this is C# 2005, isn't there a TryParse method on DateTime?

HTH,
Mythran
Aug 9 '06 #6

P: n/a

"Mythran" <ki********@hotmail.comwrote in message
news:uz**************@TK2MSFTNGP05.phx.gbl...
>
"WhiteWizard" <Wh*********@discussions.microsoft.comwrote in message
news:5E**********************************@microsof t.com...
>Have you tried using the IsDate function? By using that you can
determine if
the string you are looking at can be converted to DateTime. Here is a
link:

http://msdn2.microsoft.com/en-us/lib...3t(d=ide).aspx

HTH

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
"B.N.Prabhu" wrote:
>>Hi,

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
Aug 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.