2017-05-16 11 views
0

私は前の開発者のコ​​ードを作成しています。このコードにはSystemTimeが設定されています。 今日の日付とマイナス30日をこの形式で取得する方法はありますか?以下はVBA 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 

答えて

0

文字列として、ネイティブの日付&時間を構築-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") 
+0

アレックス、ありがとう、それは完璧に働いた! – Pang

0

SYSTEMTIMEは、あなたのコード内の別の場所で定義されたカスタムタイプのように見えます。これはAccess VBAで使用できる標準型ではありません。したがって、効果的に使用するには、定義を見つける必要があります。また、GetSystemTimeは、あなたのコード専用のカスタム関数です。同様のタイプのサンプル定義がありますが、実際にシステムに実装されているものとは異なる場合があります。http://custom-designed-databases.com/wordpress/2011/get-milliseconds-or-seconds-from-system-time-with-vba/

つまり、System TimeはWindowsシステムの時間を表します。また、Now()関数を使用してVBAにネイティブ機能を持たせることもできます。 (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") 
+0

GetSystemTime()はプラットフォームAPIで、現在の時刻をUTCで返します。 –

+0

ただし、Access VBAではネイティブでは使用できません。 OPに記述されている方法にアクセスするための関数を作成する必要があります。 – Scott

+0

あなたはそれを宣言しなければなりません、現地時間であるNow()と違ってUTC時刻を返すことを指摘してください。 –

0

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