Today, I was stumbled upon UTC time conversions. I needed to convert UTC date/time to local date/time and vice versa. I was hunting this information everywhere with no luck.

I didn’t get the straight answer anywhere. So, I gathered all the info and created two VBScript based Functions. Hope it helps someone.

Here is the formula for converting time:

Local time to UTC: local time + (Bias / 1440 ) = UTC

UTC to Local Time: UTC – (Bias/1440) = Local Time

Bias is available on the registry which tell what Time Zone is on the local computer. You can check the registry location at HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInformationActiveTimeBias

The ActiveTimeBias is the value of Bias/1400.

So, it came down to Adding the Bias to Local Time gives you the UTC time and Subtracting the Bias from UTC gives the local time.

Now the VBScript implementation of converting the times back and fourth.

‘*************************************************************************

‘ Name: ConvertToUTCTime ( sTime as Date )

‘ Parameters: Takes the Local Time. eg., 11/07/2006 10:00:00 AM

‘ Returns the converted UTC Time as 2006-11-07T18:00:000Z

‘

‘ Written by : Anand Venkatachalapathy

‘*************************************************************************

Function ConvertToUTCTime(sTime)

Dim od, ad, oShell, atb, offsetMin

Dim sHour, sMinute, sMonth, sDay

od = sTime

‘if you passed sTime as sting, comment the above line and

‘uncomment the below line.

‘od = CDate(sTime)‘Create Shell object to read registry

Set oShell = CreateObject("WScript.Shell")atb = "HKEY_LOCAL_MACHINESystemCurrentControlSet" & _

"ControlTimeZoneInformationActiveTimeBias"

offsetMin = oShell.RegRead(atb) ‘Reading the registry‘Convert the local time to UTC time

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, add zero

sDay = Day(CDate(ad))

If Len(sDay) = 1 Then

sDay = "0" & sDay

End If

‘if Hour is single digit, add zero

sHour = Hour(CDate(ad))

If Len(sHour) = 1 Then

sHour = "0" & sHour

End If

‘if Minute is single digit, add zero

sMinute = Minute(CDate(ad))

If Len(sMinute) = 1 Then

sMinute = "0" & sMinute

End If

‘Assign the reutrn value in UTC format as 2006-11-07T18:00:000Z

ConvertToUTCTime = Year(CDate(ad)) & "-" & _

sMonth & "-" & _

sDay & "T" & _

sHour & ":" & _

sMinute & ":00Z"

End Function

‘ End of Function ConvertToUTCTime

‘*************************************************************************

‘ Name: ConvertToLocalTime ( sTime as Date )

‘ Parameters: Takes the UTC Time as 2006-11-07T18:00:000Z

‘ Returns the converted Local Time. eg., 11/07/2006 10:00:00 AM

‘

‘ Written by : Anand Venkatachalapathy

‘*************************************************************************

Function ConvertToLocalTime(sTime)

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

ConvertToLocalTime = FormatDateTime(ResultDate,vbShortDate) & " " & FormatDateTime(ResultDate,vbLongTime)

End Function

‘End of ConvertToLocalTime Function

Thank you !! But does this consider Daylight Saving as well?

Hi my friend! I want to say that this article is awesome, nice written and include approximately all vital infos.

I would like to see more posts like this.

hi..good article..but i’m getting wrong date.. i have given the input same as you but im getting worng result for converttoUTCtime….i’m getting time from registry is -330

WshShell.RegRead erro ‘80070003’

Raiz inválida na chave do Registro “HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInformationActiveTimeBias”.

Freaking wordpress writer removed \es from the registry path.

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias

well written…thanks for the information

mytime=ConvertToUTCTime(“02/14/2018 02:19 AM”)

msgbox mytime

Function ConvertToUTCTime(sTime)

Dim od, ad, oShell, atb, offsetMin

Dim sHour, sMinute, sMonth, sDay

‘od = sTime

‘‘if you passed sTime as sting, comment the above line and

‘‘uncomment the below line.

od = CDate(sTime)

‘Create Shell object to read registry

Set oShell = CreateObject(“WScript.Shell”)

atb = “HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias”

offsetMin = oShell.RegRead(atb)

‘‘Reading the registry

‘‘Convert the local time to UTC time

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, add zero

sDay = Day(CDate(ad))

If Len(sDay) = 1 Then

sDay = “0” & sDay

End If

‘‘if Hour is single digit, add zero

sHour = Hour(CDate(ad))

If Len(sHour) = 1 Then

sHour = “0” & sHour

End If

‘‘if Minute is single digit, add zero

sMinute = Minute(CDate(ad))

If Len(sMinute) = 1 Then

sMinute = “0” & sMinute

End If

‘‘Assign the reutrn value in UTC format as 2006-11-07T18:00:000Z

ConvertToUTCTime = Year(CDate(ad)) & “-” & _

sMonth & “-” & _

sDay & “T” & _

sHour & “:” & _

sMinute & “:00Z”

End Function

Please explain the below one in second function,what it is trying to evaluate

od = CDate( _

Mid(sTime,9,2) & “-” & _

Mid(STime,6,2) & “-” & _

Mid(STime,1,4) & ” ” & _

Mid(STime,12,8) _

)