我正在编写前一个开发人员的代码。此代码已设置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发布于 2017-05-16 16:23:53
生成本机日期和时间,加-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")发布于 2017-05-16 16:31:36
SYSTEMTIME似乎是代码中其他地方定义的自定义类型。它不是Access VBA中可用的标准类型。因此,要有效地使用它,您需要找到定义。另外,GetSystemTime也可能是您的代码独占的自定义函数。下面是类似类型的示例定义,尽管它可能不是在您的系统中实现的:http://custom-designed-databases.com/wordpress/2011/get-milliseconds-or-seconds-from-system-time-with-vba/
也就是说,系统时间指的是Windows系统时间。在VBA中还具有使用Now()函数获得时间的本机能力。(https://msdn.microsoft.com/en-us/library/office/gg278671.aspx)这将返回一个类型为Date的变量,该变量等价于整数表示天数而十进制代表一天中的时间的数字。今天前30天的一个例子是:
Dim lastMonth as Date
Dim formattedDate as String
lastMonth = Now() - 30
formattedDate = Format(lastMonth, "yyyy-mm-ddThh:nn:ssZ")发布于 2017-05-17 08:43:48
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 Functionhttps://stackoverflow.com/questions/44006568
复制相似问题