Search code examples
ms-accessvbasystemtime

VBA SystemTime - 30 days


I am working on a code from a previous developer. This code has SystemTime set up. Is there a way to get today date and minus 30 days in this format?

Code Below:

Public Function GetIsoTimestampTest() As String
Dim st As SYSTEMTIME

'Get the local date and time
GetSystemTime st

'Format the result
GetIsoTimestampTest = _
    Format$(st.wYear, "0000") & "-" & _
    Format$(st.wMonth, "00") & "-" & _
    Format$(st.wDay, "00") & "T" & _
    Format$(st.wHour, "00") & ":" & _
    Format$(st.wMinute, "00") & ":" & _
    Format$(st.wSecond, "00") & "Z"
End Function

Solution

  • Build a native date & time, add -30 days, format as a string:

    utcInIsoFormat = Format$(DateAdd("d", -30, _
        DateSerial(st.wYear, st.wMonth, st.wDay) _
        + TimeSerial(st.wHour, st.wMinute, st.wSecond)), "yyyy-mm-ddThh:nn:ssZ")