vba date from string with format code example

Example 1: vba string to date

Dim myDate As Date, strDate As String
myDate = CDate("2021-01-28")
strDate = Format(myDate, "YYYY-MM-DD")				  ' 2021-01-28
' String to date
myDate = CDate(Format$("20210213101235", "####-##-## ##:##:##"))
' Date to string
strDate = Format(myDate, "YYYY-MM-DD hh:mm:ss")       ' 2021-02-13 10:12:35

Example 2: vba date format

Dim myDate As Date, strDate As String
myDate = CDate("2021-01-28")			' Date
strDate = Format(myDate, "YYYY-MM-DD")	' String
strDate = Format(myDate, "DD/MM/YYYY hh:mm:ss")

Tags:

Vb Example