Ms access VBA系统时间-30天

Ms access VBA系统时间-30天,ms-access,vba,systemtime,Ms Access,Vba,Systemtime,我正在编写以前的开发人员编写的代码。此代码已设置SystemTime。 有没有办法用这种格式得到今天和减去30天的日期 代码如下: Public Function GetIsoTimestampTest() As String Dim st As SYSTEMTIME 'Get the local date and time GetSystemTime st 'Format the result GetIsoTimestampTest = _ Format$(st.wYear, "0

我正在编写以前的开发人员编写的代码。此代码已设置SystemTime。 有没有办法用这种格式得到今天和减去30天的日期

代码如下:

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

生成本机日期和时间,添加-30天,格式为字符串:

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

SYSTEMTIME似乎是代码中其他地方定义的自定义类型。它不是Access VBA中可用的标准类型。因此,为了有效地使用它,您需要找到定义。此外,GetSystemTime也可能是代码专用的自定义函数。下面是一个类似类型的示例定义,尽管它可能与您的系统中实现的不完全相同:

也就是说,系统时间指的是Windows系统时间。您还可以在VBA中使用Now()函数获取时间。()这将返回一个类型为Date的变量,该变量相当于一个数字,其中整数表示天,小数表示一天中的时间。在今天之前30天的一个例子是:

Dim lastMonth as Date
Dim formattedDate as String    

lastMonth = Now() - 30
formattedDate = Format(lastMonth, "yyyy-mm-ddThh:nn:ssZ")

DateSerial愉快地接受负天数计数。因此:

Public Function IsoDateMinus30() As Date

    Dim st As SYSTEMTIME
    Dim Result As Date

    ' Get the local date and time
    GetSystemTime st

    Result = DateSerial(st.wYear, st.wMonth, st.wDay - 30)

    ' To include time:
    '
    ' Result = _
    '     DateSerial(st.wYear, st.wMonth, st.wDay - 30) + _
    '     TimeSerial(st.wHour, st.wMinute, st.wSecond)    

    IsoDateMinus30 = Result

End Function

GetSystemTime()是一个平台API,它以UTC为单位返回当前时间。但在Access VBA中本机不可用。你必须创建一个函数,以OP描述的方式访问它。你只需要声明它,它返回UTC时间,而不是Now(),后者是本地时间。