1

this is my input to a text box from a datetime calendar and to store the datetime in the DB i want the value to be changed to Date time.

DateTime Res_date = Convert.ToDateTime(txt_RespondBy.Text); 

when I convert it gives a error saying string not recognised as valid date time. How to convert the input to date time..

value of txt_RespondBy.Text is : 10/27/2011 03:25

2

5 Answers 5

2

use DateTime.Parse, see MSDN: DateTime.Parse Method (String)

string[] dateStrings = {"2008-05-01T07:34:42-5:00", 
                        "2008-05-01 7:34:42Z", 
                        "Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
   DateTime convertedDate = DateTime.Parse(dateString);
   Console.WriteLine("Converted {0} to {1} time {2}.", 
                     dateString, 
                     convertedDate.Kind.ToString(), 
                     convertedDate);
}

There is Also ParseExact where you can specify the pattern string you are using....

Sign up to request clarification or add additional context in comments.

Comments

2

Use DateTime.TryParseExact to avoid exceptions.

http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx

Comments

1

Use DateTime.ParseExact with the correct format string.

Comments

1

You should use DateTime.Pares or DateTime.ParseExact

Comments

1

What is the format of the string you're trying to parse? Anyway, take a look at DateTime.ParseExact(): http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

It allows you to provide a format in which the date is represented.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.