J.Leo J.Leo - 1 year ago 56
Vb.net Question

String value wrongly converted to datetime using vb.net

So I have this datetime value of 9.3.2016 18:56:12, by using datetime.parse, I can get the values but instead of getting '3' as the month, it takes '9' as month and '3' as day which is incorrect.

dim d1 as string = "9.3.2016 18:56:12"
dim d2 as datetime = datetime.parse(d1, CultureInfo.InvariantCulture)

I don't want to use datetime.parseExact because I'm having more than 1 value in the database.

Please help :( thank you!

Answer Source

Since you current culture appears to interpret dates in the way you expect you could simply do this,

Dim dateString = "9.3.2016 18:56:12"
Dim dateValue = DateTime.Parse(dateString)

By not specifying the InvariantCulture, you instruct DateTime.Parse to use the current culture which, in your case, interprets the date string correctly.