Search code examples
vbams-access

Convert Date object to String formatted as "YYYY-MM-DD" instead of defaulting to system format


I need to convert a Date to a string in the format "YYYY-MM-DD" (In Access VBA). The issue that I am having is that different users on different computers will have different date formats. I first tried the following:

dim dateString As String
dateString = Format(Date,"yyyy-mm-dd")

However, if the user's computer settings have their date format as "yyyy/mm/dd" (or any other date format), then it will convert to that rather than "yyyy-mm-dd" as desired.

I've tried solutions such as this, however, it still reverts back to the user's date format.

Also, as an aside, I'm aware that Access stores dates in a numeric fashion, and I am not attempting to change how the data is stored, I simply want to convert a date object to a string formatted as "yyyy-mm-dd".


Solution

  • I can understand you want to change just visually, try this

    Good Luck

    Sub MyCustomDisplayDate()
        Dim dateString As String
        Dim dateDate As Date
      
        dateDate = Now
        dateString = Format(dateDate, "yyyy") & "-" & Format(dateDate, "MM") & "-" & Format(dateDate, "dd")
    End Sub