0

I am getting error in converting string to date. I don't want to use calendar control because date has to generate at runtime and textbox and not editable.

error

String was not recognized as a valid DateTime."

ASP

txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/m/yyyy");

C# class

_assessmentRemidationObject.RemediationRecommendedDate = Convert.ToDateTime(txtBoxRemediationDate.Text);

Model

public DateTime RemediationRecommendedDate { get; set; }

1 Answer 1

1

Your code above is currently setting the Month element to minutes, so:

txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/m/yyyy");

Sets the value to "14/23/2015" (an invalid date) at 17:23 on 14/09/15.

Try changing "m" to "M":

txtBoxRemediationDate.Text = System.DateTime.Now.ToString("d/M/yyyy");

This sets the value to the correctly formatted date today: "14/9/2015".

Have a look here for more formatting: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

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

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.