Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have string in format 24/01/2011 in day/month/year format.

I want to convert it into date time format.

In asp.net I did something like this:
C#
DateTime Dob = Convert.ToDateTime(TextBox2.Text);

but it's showing as string is not in date time format.
Now I am giving complete code in front end I did something as
<asp:TextBox ID="TextBox2" runat="server">
        <cc1:CalendarExtender ID="TextBox2_CalendarExtender"  runat="server" 
            Enabled="True" Format="dd/MM/yyyy" TargetControlID="TextBox2">
        
        <asp:RegularExpressionValidator ID="RXE" ControlToValidate="TextBox2"
        ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d" runat="server" ErrorMessage="RegularExpressionValidator">
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label">

and in code behind
as
DateTime Dob = DateTime.Parse(TextBox2.Text);
        Label1.Text = Dob.ToString();


and the error is as
String was not recognized as a valid DateTime.

Please help me sort out the issue.

My problem only in 24/01/2010.


Thanking you
Mohammad wasif
Posted
Updated 24-Jan-11 22:38pm
v6
Comments
Dalek Dave 25-Jan-11 4:16am    
Edited for Readability.
Mohd Wasif 25-Jan-11 6:01am    
Solved Thanks Guys

If you run the code you give:
DateTime Dob = Convert.ToDateTime(TextBox2.Text);
Then "Dob" will contain the date in DateTime format - it can't hold anything else.
However, whenever you try to print it: to the console, or a label, or a textbox, or even view it in the debugger, there is an implicit call to the ToString method to make it work.
It is still stored in DateTime: Try replacing "Dob" with "Dob.Month" and you will see just the month appear.
 
Share this answer
 
Comments
Dalek Dave 25-Jan-11 4:19am    
Good Spot.
Mohd Wasif 25-Jan-11 4:28am    
no i am not talking about that.Actually when I am entering date like 24/01/2011 in textbox and in code behind
DateTime Dob = Convert.ToDateTime(TextBox2.Text);
Label1.Text=Dob.Tostring();
its showing string in incorrect form.
OriginalGriff 25-Jan-11 4:37am    
In which case it is probably a Locale problem: try using
DateTime Dob = DateTime.ParseExact(textBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture );
To specify the format you will receive.
Refer this [^]
 
Share this answer
 
Comments
m@dhu 25-Jan-11 3:34am    
good one.
Pravin Patil, Mumbai 25-Jan-11 3:41am    
Very helpful URL..
Sandeep Mewara 25-Jan-11 4:14am    
Good link! 5!
Dalek Dave 25-Jan-11 4:18am    
Good Link.

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