0

First, do not get confused; this is NOT asking to convert from the current time zone to UTC. Not at all.

What I need is to convert from a PROVIDED time zone by name (e.g. "Pacific Standard Time") to UTC.

The conversion must be taking into account the daylight saving offset for the provided date.

For example, if I enter 03/10/2016 6:00 PM Pacific Standard Time, it must return 04/10/2016 1:00 AM because at that date the time zone offset is -7 hours.

I've searched up and down the internet for this and couldn't find anything. If this was a different language it wouldn't be an issue, but VBScript seems pretty limited.

I would like to avoid 3rd party libraries and stick with standard MS-provided objects if possible.

Please advise.

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • 1
    It's a manual process I'm afraid, VBScript doesn't support timezones. So you have to implement something yourself using the in-built Date/Time functions. Probably would [store the offsets in a database](http://stackoverflow.com/questions/24797/effectively-converting-dates-between-utc-and-local-ie-pst-time-in-sql-2005) using a table of time-zones and power it using that. – user692942 Nov 03 '16 at 15:25
  • 1
    I'd also recommend reading this answer - [A: Daylight saving time and time zone best practices](http://stackoverflow.com/a/2532962/692942) - useful quote to remember - *"Do not confuse a "time zone", such as America/New_York with a "time zone offset", such as -05:00. They are two different things."*. also lots of useful info on the [tag:timezone] tag [info page](http://stackoverflow.com/tags/timezone/info). – user692942 Nov 03 '16 at 15:32
  • VBScript is a language, but it's used in a lot of different environments, which can effect your available options. For example, if you can shell execute, you can [play with some embedded powershell](http://stackoverflow.com/a/39452920/634824). If you're in a classic ASP environment you might P/Invoke a COM object, etc. Options may be different elsewhere. Can you tell us more about your environment where VBScript is running? – Matt Johnson-Pint Nov 04 '16 at 00:34

2 Answers2

0

it's difficult with vbscript. You could get TimeZone info (Standard Offset) via parsing the output of TZutil.exe but this won't give you neither DST-Bias nor the info if DST is in effect at a specific date. You could do a wmi query in vbscript and this returns that information but only for the active time zone of the current pc. The information sits in windows but I don't know a way to get them via vbscript.

Powershell on the other hand exposes much of this data. I don't know the date when DST ends in Pacific Standard Time, but Powershel/.Net does. See these lines entered in a PoSh console:

> $PacificDateTime = Get-Date "2016/11/05 11:00:00"
> [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(($PacificDateTime),'Pacific Standard Time','UTC')
Freitag, 5. November 2016 18:00:00

> $PacificDateTime = Get-Date "2016/11/15 11:00:00"
> [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(($PacificDateTime),'Pacific Standard Time','UTC')
Dienstag, 15. November 2016 19:00:00

The first date has a time difference of -7h while it normally is -8h, so DST is in effect. The second date 10 days ahead has an -8h difference so I guess the switch is in the mean time. This could easily be converted to function. But I'll leave a bit of work to you.

0

Well, in the end VBScript didn't provide the required feature. So I had to implement it on a Web API and call it from my local program. I used the TimeZoneInfo.ConvertTimeToUtc() which does exactly what I need. Then I called it like this: https://stackoverflow.com/a/22328840/442512

Community
  • 1
  • 1
Emmanuel
  • 16,791
  • 6
  • 48
  • 74