将时间转换为 UTC vbScript [英] Convert Time to UTC vbScript

查看:20
本文介绍了将时间转换为 UTC vbScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数可以很好地将当前时间转换为 UTC 时间.

I have the following function which does fine at converting current time to UTC time.

Function toUtc(byVal dDate)
    Dim oShell : Set oShell = CreateObject("WScript.Shell")
    toUtc = dateadd("n", oShell.RegRead("HKEY_LOCAL_MACHINESystemCurrentControlSetControlTimeZoneInformationActiveTimeBias"), cDate(dDate))
End Function

但是,我认为这不能充分处理未来或历史日期到 UTC 的转换,因为该函数需要知道转换日期时服务器时区的偏移量,以及是否不是在夏令时期间.

However, I am thinking that this does not adequately handle conversion of future or historical dates to UTC, since the function would need to know the offset of the server's timezone at the time of the date that is being converted, and whether or not it was during daylight savings time or not.

我该如何解决这个问题?

How can I get around this?

我在带有 IIS7.5 的 Windows 服务器上使用 vbScript/Classic ASP

I am using vbScript/Classic ASP on windows server with IIS7.5

澄清一下,这与格式化日期无关.这完全是关于从服务器时区转换为历史日期的 UTC.在夏令时期间,如果我尝试转换以标准时间出现的日期时间,则偏移量将减少 60 分钟.

To clarify, this has nothing to do with formatting the date. It is all about converting to UTC from the server timezone for historical dates. During Daylight savings time, the offset will be off by 60 minutes if I try to convert a datetime which occurred in standard time.

例如:

假设今天,2013 年 2 月 19 日(非夏令时),我想将时间戳从 2008 年 6 月 5 日(夏令时期间)从 PDT(我的服务器时区)转换为 UTC.在我的函数中使用该方法会给我一个与正确值相差 60 分钟的值(因为当前时间是 PST(不是 PDT),因此该历史日期的偏移量将不正确).

Lets say that today, 2013-02-19 (non daylight-savings time), I want to covert a timestamp from say, 2008-06-05 (during daylight savings period) from PDT (my server timezone) to UTC. using the method in my function will give me a value which is 60 minutes off from the correct value (because the CURRENT time is PST (not PDT), the offset will be incorrect for that historical date).

换句话说,输入日期的时区偏移量可能是 UTC-7 或 UTC-8,具体取决于在该特定日期是否观察到 DST.我需要为输入日期计算正确的 UTC 日期,并且无论当前日期是否观察到 DST,我都需要代码工作.

In other words, the timezone offset of the input date could be UTC-7 or UTC-8 depending on whether or not DST was observed on that particular date. I need to calculate the correct UTC date for the input date and I need the code to work whether or not DST is observed on current date.

推荐答案

这是我在@AardVark71 的建议下最终实施的解决方案.

Here is the solution I ended up implementing at the suggestion of @AardVark71.

我把它放在我的 func.asp 文件中:

I put this in my func.asp file:

<script language="javascript" runat="server">
    function toUtcString(d) {
        var dDate = new Date(d);
        return Math.round(dDate.getTime() / 1000);
    };
</script>

它输出一个时间戳值.然后从经典 asp 代码中的任何地方,我都可以这样做:

It outputs a timestamp value. Then from anywhere in the classic asp code, I can do this:

response.Write DateAdd("s", toUtcString(cDate("11/11/2012 06:25 PM")), "01/01/1970 00:00:00") 'expect 11/11/2012 10:25:00 PM gmt/utc time
response.Write DateAdd("s", toUtcString(cDate("06/11/2012 06:25 PM")), "01/01/1970 00:00:00") 'expect  6/11/2012  9:25:00 PM gmt/utc time

这(我相信)很简单,它会在 DST 中产生预期值和完全因素.

This (I believe) is simple, and it produces the expected values and fully factors in the DST.

这篇关于将时间转换为 UTC vbScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆