I am trying to get the Date as a string formatted yyyy-mm-dd
.
I have tried various things with strange results:
Dim mydate As String
mydate = Date
mydate = Year(Date)
mydate = Month(Date)
mydate = Day(Date)
11/02/
without the year.I can try to concatenate the data but:
Any clarification or an example would be very welcome.
In VBA, there is a method through which we can convert a given string to a date. The method is known as the CDATE function in VBA. It is an inbuilt function in VBA, and the parts required for this function are first to convert the string to a number, then convert the given number to date.
To format a date in VBA, we use the inbuilt FORMAT function itself. It takes input as the date format and returns the desired format required. The arguments required for this function are the expression and the format type.
Use the Format
function from the VBA.Strings
built-in module:
Debug.Print Format(Now, "YYYY-MM-DD")
Dim sToday As String
sToday = CStr(Date)
That gives sToday value, e.g. "2020-12-31", in the format of my system's date.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With