Skip to content

Instantly share code, notes, and snippets.

@leonardocintra
Created July 12, 2017 13:09
Show Gist options
  • Save leonardocintra/81ab6b39114e235152adc928215c323d to your computer and use it in GitHub Desktop.
Save leonardocintra/81ab6b39114e235152adc928215c323d to your computer and use it in GitHub Desktop.
Converter UTC to GMT3 (ou local time)
Function ConvertUTCToLocalTime(sTime)
'Fonte: https://anandthearchitect.com/2006/11/08/convert-utc-to-local-time-and-vice-versa-using-vbscript/'
Dim od, ad, oShell, atb, offsetMin
Dim sHour, sMinute, sMonth, sDay
Dim ResultDate
'Convert the UTC time 2006-11-07T18:00:000Z to
'a normal date format 11/07/2006 10:00:00
'Note the converted date would be 24 hr format
od = CDate( _
Mid(sTime,9,2) & "-" & _
Mid(STime,6,2) & "-" & _
Mid(STime,1,4) & " " & _
Mid(STime,12,8) _
)
'Create Shell object to read registry
Set oShell = CreateObject("WScript.Shell")
atb = "HKEY_LOCAL_MACHINESystemCurrentControlSet" & _
"ControlTimeZoneInformationActiveTimeBias"
offsetMin = oShell.RegRead(atb) 'Reading the registry
'Make the Bias as negative value. This step is required,
'because VBScript offers only DateAdd function. I wish,
'there is DateSubtract function in VBScript.
offsetMin = -(offsetMin)
'Convert the UTC time to Local Time,
'Note the OffsetMin is negative value
ad = dateadd("n", offsetMin, od)
' If Month is single digit value, add zero
sMonth = Month(CDate(ad))
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
' If Day is single digit value, add zero
sDay = Day(CDate(ad))
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
' If Hour is single digit value, add zero
sHour = Hour(CDate(ad))
If Len(sHour) = 1 Then
sHour = "0" & sHour
End If
' If Minute is single digit value, add zero
sMinute = Minute(CDate(ad))
If Len(sMinute) = 1 Then
sMinute = "0" & sMinute
End If
' Gather up all fields and convert to a Date format
ResultDate = cDate(sDay & "-" & sMonth & "-" & Year(CDate(ad)) & " " & sHour & ":" & sMinute & ":00")
'In the above step, ResultDate is in 24 hour format.
'So convert into 12 hr format and assign the return value
ConvertUTCToLocalTime = FormatDateTime(ResultDate,vbShortDate) & " " & FormatDateTime(ResultDate,vbLongTime)
End Function
'End of ConvertUTCToLocalTime Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment