首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA SystemTime - 30天

VBA SystemTime - 30天
EN

Stack Overflow用户
提问于 2017-05-16 16:12:56
回答 3查看 1.4K关注 0票数 0

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

代码如下:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-16 16:23:53

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

代码语言:javascript
复制
utcInIsoFormat = Format$(DateAdd("d", -30, _
    DateSerial(st.wYear, st.wMonth, st.wDay) _
    + TimeSerial(st.wHour, st.wMinute, st.wSecond)), "yyyy-mm-ddThh:nn:ssZ")
票数 0
EN

Stack Overflow用户

发布于 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天的一个例子是:

代码语言:javascript
复制
Dim lastMonth as Date
Dim formattedDate as String    

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

Stack Overflow用户

发布于 2017-05-17 08:43:48

DateSerial高兴地接受了一天的负数。因此:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44006568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档