davin kurniadi davin kurniadi - 10 days ago 5
Vb.net Question

make the specific number to become day of month

so i have this problem, i want to make specific number to become a day of month.
example: i had an output of label

5
now how to make it to become
05-12-2016
? i just convert them into string like this

dim testing as string = ""
testing = Now.Year.ToString & Right("0" & Now.Month.ToString, 2) & Right("0" & "5" 2)


output :

2016-11-05


and if i use string, i cannot compare it with
date.Now


thanks before

Answer

DateTime has a constructor that takes the year, month and day of month as parameters that you can use to create an instance:

Dim dayOfMonth As String = "5"
Dim myDate = New Date(Date.Now.Year, Date.Now.Month, CInt(dayOfMonth))

You can use the ToString method to format the date however you need:

Dim format1 As String = myDate.ToString("yyyy-MM-dd")
Dim format2 As String = myDate.ToString("dd-MM-yyyy")