Click here to Skip to main content
16,018,394 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
I have two textboxes one contains date from database and another is to append time into it.

C#
TextBox Date = new TextBox();
                Date.Text = "11/05/2015"; //dd/MM/yyyy
                TextBox txt1 = new TextBox();
                txt1.Text = "12:00:00";


I have Concatenated the two textboxes to generate standard datetime string as follows.

C#
var str1 = Date.Text+'.'+txt1.Text;


and I have checked that sting is returning the same value i.e. "11/05/2015 12:00:00"

Now I'm converting the string value to datetime.
I'm doing like this.

C#
var d1 = DateTime.ParseExact(str1, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var t1 = d1.ToString("MM/dd/yyyy HH:mm:ss");


But every time it shows the error "string was not recognized as a valid DateTime".
Posted
Updated 21-May-15 4:06am
v3

This works:

C#
string str = "11/05/2015 12:00:00";
var d1 = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);


This works as well:

C#
string str = "11/05/2015 12:00:00";
var d1 = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss",
null);


You can test both of them here:
http://csharppad.com/[^]
 
Share this answer
 
v2
Comments
arunsiddhu1992 22-May-15 1:58am    
Thanks.
The problem was in database value.
It was 11/52015.
That's why I encountered error.
Thanks for the help.
Change this:
var str1 = Date.Text+' '+txt1.Text;
 
Share this answer
 
Comments
arunsiddhu1992 22-May-15 1:57am    
yes I got it it was adding the dot(). to.
Thanks for the help.

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