Alex Alex - 3 months ago 13
Vb.net Question

VB.Net Convert Date format from yy-mm-dd back to standart date format (dd.mm.yyyy)

So I'm running in a bit of trouble. I'm trying to convert a date format of yy-mm-dd back to dd.mm.yyyy. I tried to do it with lookup but i can get how to get only the second entry of this.

Dim lookupDateYear = DateString.ToLookup(Function(l) If(Len(l) < 2, l, Mid(l, 1, 2)), Function(l) Split(l, "-", 3).first)
Dim lookupDateDay = DateString.ToLookup(Function(l) If(Len(l) < 2, l, Mid(l, 1, 2)), Function(l) Split(l, "-", 3).last)

Answer

You're definitely doing this the hard way. This should work much better:

DateTime.ParseExact(DateString, "yy-MM-dd", Nothing).ToString("dd/MM/yy")

Note that the / character in here is a placeholder for the Date Separator Character defined for the given culture (In this case, Nothing maps to your current default culture. The Invariant culture is also often useful). If the culture settings have defined . as the date separator, you'll see . instead of a / in the output. If you want a . regardless of the date separator character, just change that to use the . instead of / in the format string, or use a CultureInfo, DateTimeFormatInfo, or IFormatProvider object where . is the date separator character.

Depending on how you plan to use this, you may also do better just keeping the date object, instead of converting it back into a string after parsing.

Comments