kiLLua kiLLua - 3 months ago 7
Vb.net Question

convert Integers to Date Objects VB

I have 2 integers and 1 String. Now I want to convert it in date objects and display it in datepicker1 (I set its format to "short")

for example:

Dim Day As Integer = 14
Dim Month As String = "July"
Dim Year As Integer = 2016


I am expecting an answer as

7/14/2016 'this must be displayed in datepicker1


I searched through the internet and i found the same question here but this is from different language and i want it in VB. if my question has already been asked before. please post the link.

I am new to vb so i am hoping for your help guys!

Answer

Here's a way that uses Date.ParseExact:

Dim dt = Date.ParseExact($"{Year}-{month}-{day}", "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)

Note that i'm using string interpolation at $"..." which is available in VS 2015/VB14. Otherwise you could use String.Format:

Dim dtStr = String.Format("{0}-{1}-{2}", Year, month, day)
Dim dt = Date.ParseExact(dtStr, "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)

Here you'll find more informations about the custom date and time format strings.