0

I have this code:

Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & "/" & DayBirth.SelectedValue.ToString & "/" & YearBirth.SelectedValue.ToString
Dim birthday As DateTime = Convert.ToDateTime(birthdaystring)

Which produces errors (String was not recognized as a valid DateTime.)

The string was "01/31/1963".

Any assistance would be appreciated.

Thanks.

3 Answers 3

2

The problem is probably that the culture used for parsing the date expects a MM/dd/yyyy format rather than dd/MM/yyyy format.

Instead of creating a string and parsing it, create the DateTime value directly:

Dim birthday As New DateTime(YearBirth.SelectedValue, MonthBirth.SelectedValue, DayBirth.SelectedValue)
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of creating a string which you later try to parse to a date try this:

Dim birthday As DateTime = new DateTime(_
    CType(YearBirth.SelectedValue, Integer), _
    CType(MonthBirth.SelectedValue, Integer), _
    CType(DayBirth.SelectedValue, Integer))

Comments

1

Try changing it to:

DateTime.Parse(birthdaystring);

I'm guessing it will work, but if not - you can add a second parameter to the parse saying the format you input, i.e.:

DateTime.Parse(birthdaystring,"MM/dd/yyyy");

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.