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

查看:230
本文介绍了转换时间为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_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias"), 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 Server

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-06-05(夏令时期间)时间戳从PDT(我的服务器时区)为UTC。用我的函数方法会给我一个值是60分钟关闭从正确的值(因为当前时间是太平洋标准时间(PDT没有),偏移量将是不正确对于历史的日期)。

For example: 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).

推荐答案

下面是我结束了在@ 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 code的任何地方,我可以这样做:

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天全站免登陆