开发者

String / DateTime Conversion problem (asp.net vb)

开发者 https://www.devze.com 2022-12-23 18:27 出处:网络
I have this code: 开发者_开发问答Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & \"/\" & DayBirth.SelectedValue.ToString & \"/\" & YearBirth.SelectedValue.ToString

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.


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)


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))


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消