AccessVBA 世界標準時間を日本時間に変換
VBA 世界標準時間を日本時間に変換する関数
注意点
・日付型と数値型の扱い
VBAでは日付は内部的に数値として扱われる。1日が1.0として計算されるため、時間を計算する際には小数部分を扱う。例えば、1時間は1/24、1分は1/1440、1秒は1/86400となる。
' UTCをJSTに変換 ' 引数 : strUTC(文字列) | 例: "2023-05-11 00:10:07 UTC" Public Function ConvertUTCToJST(strUTC As String) As Date dim dtUTC as Date strUTC - Replace(strUTC, " UTC","") dtUTC = CDate(strUTC) ConvertUTCToJST = dtUTC + 9 / 24 End Function
Accessでの使用例
UTCのフィールド[SentDate]から時間を変換しJSTのフィールドへ入れる
Public Function UpdateJPTimes(sTableName As String) As Boolean Dim rst As DAO.Recordset Dim strSent As String Dim strJPTime As String On Error GoTo ErrHandler Set rst = CurrentDb.OpenRecordset(sTableName) Do While Not rst.EOF rst.Edit If Not IsNull(rst![SentDate]) Then strSent = rst![SentDate] strJPTime = ConvertUTCToJST(strSent) rst![JPTIME] = strJPTime rst.Update End If rst.MoveNext Loop rst.Close Set rst = Nothing UpdateJPTimes = True ErrHandler: MsgBox Err.Description, vbCritical UpdateJPTimes = False End Function