Kimball Robinson
Kimball Robinson

Reputation: 3387

vbscript time zones and Daylight Savings

I'm looking for an easy way to get UTC times and date/time information in vbscript, even while specifying a time zone. I need to be able to get the current time in a time zone of my choice.

I have seen postings on the web for functions that determine DST, but I'd rather not use something I'd have to update if DST or time zones switched--so using a standard library would be ideal. I don't have the option of using a web service with this application. I'm open to other ideas, though.

Upvotes: 2

Views: 8229

Answers (2)

There is a little anomaly, because in october from 02:00 to 03:00 AM the clock shows the time, then after 03:00, shows 02:00 once again, so who knows 02:30 is the first 02:30, or the second one? Anyway, this function counts the UTC time, from local time. The DST begins at 02:00 last Sunday of March and ends at 03:00 last Sunday of Oct.

function UTC(thisdate, zonediff)
    '        CET   |            CEST    |      CET
    'UTC           03V 01:00            10V 01:00
    'IDO           03V 02:00 -> 03:00   10V 03:00 -> 02:00          
    '   UTC+1:00   |        UTC+2:00    | UTC+1:00

    DSTbegin=Year(thisdate) & "-03-" & (32-weekday(Year(thisdate)&"-03-31") ) & " 02:00:00"
    DSTend=Year(thisdate) & "-10-" & (32-weekday(Year(thisdate)&"-10-31") ) & " 03:00:00"
    if DateDiff("s", DSTbegin, thisdate)>=0 and DateDiff("s", DSTend, thisdate)<=0 then
        UTC= DateAdd("h", -zonediff-1, thisdate)
    else
        UTC= DateAdd("h", -zonediff, thisdate)
    end if
end function

I am living in Hungary, in UTC+1:00, so zonediff is +1. The format of date depends on your local timezone settings. This test shows VBA test of the DST time points in this year.

UTC(now,1), _                “2023. 06. 30. 13:14:21”
UTC("2023-03-26 01:59:59",1) “2023. 03. 26. 0:59:59”
UTC("2023-03-26 02:00:00",1) “2023. 03. 26. 0:00:00”
UTC("2023-03-26 02:00:01",1) “2023. 03. 26. 0:00:01”
UTC("2023-10-29 02:59:59",1) “2023. 10. 29. 0:59:59”
UTC("2023-10-29 03:00:00",1) “2023. 10. 29. 1:00:00”
UTC("2023-10-29 03:00:01",1) “2023. 10. 29. 2:00:01”

Upvotes: 0

Kimball Robinson
Kimball Robinson

Reputation: 3387

If you have access to a database you could use a database query to determine time zones.

Upvotes: 1

Related Questions