Option Explicit
Private Const EasternTime As Integer = 13
Private Const CentralTime As Integer = 14
Private Const MountainTime As Integer = 15
Private Const PacificTime As Integer = 16
Private Const AlaskaTime As Integer = 17
Private Const HawaiiTime As Integer = 18
Private Function TOBEIJINGTIME(ByVal USATIMEZONE As Integer, ByVal Adate As Date) As Date 'TRANS A USER TIME in a certain USA timezone TO BEIJING TIME
Dim DSTstartday As Date, DSTendday As Date
DSTstartday = DateSerial(Year(Adate), 4, (8 - Weekday(DateSerial(Year(Adate), 4, 1), vbSunday)) Mod 7 + 1) '四月的第一个星期日开始
DSTendday = DateSerial(Year(Adate), 10, (8 - Weekday(DateSerial(Year(Adate), 10, 1), vbSunday)) Mod 7 + 1) '十月的最后一个星期日结束
If Adate > DSTstartday And Adate < DSTendday Then USATIMEZONE = USATIMEZONE - 1
TOBEIJINGTIME = Format(DateAdd("h", USATIMEZONE, Adate), "YYYY-MM-DD HH:NN:SS")
End Function
Private Sub Command1_Click()
MsgBox TOBEIJINGTIME(17, Now)
End Sub
Public Const EasternTime As Interger = 13
Public Const CentralTime As Interger = 14
Public Const MountainTime As Interger = 15
Public Const PacificTime As Interger = 16
Public Const AlaskaTime As Interger = 17
Public Const HawaiiTime As Interger = 18
Public Function US_BJ_Time(Byval Americ_Time_Zone As Integer, Byval Timer_Value As Date) As Date
Dim FirstDay As Date, LastDay As Date
US_BJ_Time = DateAdd("h", Americ_Time_Zone, Timer_Value)
'Adjust For Daylight Saving Time
'The first day for Daylight Saving Time in a year is the first Sunday in April (2 AM)
For FirstDay= DateSerial(Year(Timer_Value), 4, 1) To DateSerial(Year(Timer_Value), 4, 7)
If Weekday(FirstDay) = vbSunday Then Exit For
Next
FirstDay = DateAdd("h", 2, FirstDay)
'The last day for Daylight Saving Time in a year is the last Sunday in October (2 AM)
For LastDay= DateSerial(Year(Timer_Value), 10, 25) To DateSerial(Year(Timer_Value), 10, 31)
If Weekday(LststDay) = vbSunday Then Exit For
Next
LastDay = DateAdd("h", 2, LastDay)
If Timer_Value > FirstDay And Timer_Value < LastDay Then
US_BJ_Time = US_BJ_Time - 1/24
End If
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Private Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF
Private Const TIME_ZONE_ID_STANDARD& = 1
Private Const TIME_ZONE_ID_UNKNOWN& = 0
Private Const TIME_ZONE_ID_DAYLIGHT& = 2
Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
Private Function TOBEIJINGTIME(ByVal ADATE As Date) As Date 'TRANS A USER TIME WITH CONTROLPANEL ZONE TO BEIJING TIME
Dim tZone As TIME_ZONE_INFORMATION
Dim DIFF As Long
GetTimeZoneInformation tZone
DIFF = -tZone.Bias
If TIME_ZONE_ID_DAYLIGHT And tZone.DaylightDate.wMonth <> 0 Then DIFF = DIFF - tZone.DaylightBias
TOBEIJINGTIME = Format(DateAdd("N", 8 * 60 - DIFF, ADATE), "YYYY-MM-DD HH:NN:SS")
End Function
Private Sub Command1_Click()'先用日期托盘设置一个美国时区时间,再执行之。
MsgBox TOBEIJINGTIME(Now)
End Sub
Dim hourmei As Integer '美国时间
Dim west As Boolean '是否是西部
Dim hourzhong As Integer '中国时间
Dim xsz As Boolean '是否是夏时制
if now>"夏时制开始时间" and now<"夏时制结束时间" then
xsz=true
else
xsz=false
end if
If xsz Then
If west = True Then
hourzhong = (hourmei + 12) Mod 24
Else
hourzhong = (hourmei + 11) Mod 24
End If
Else
If west = True Then
hourzhong = (hourmei + 13) Mod 24
Else
hourzhong = (hourmei + 12) Mod 24
End If
End If