经典ASP - 服务器时间与本地时间

时间:2022-04-07 01:57:44

I have a Classic ASP application that I am working with date cut offs. My server resides in Central Time, but I am in Eastern time. What happens is my app thinks it is an hour earlier and my cut offs are an hour late. I am sure they would be 2 hours early if a user was in Pacific time.

我有一个经典的ASP应用程序,我正在使用日期截止。我的服务器位于中部时间,但我在东部时间。会发生什么事情是我的应用程序认为它是提前一小时,我的截止时间晚了一个小时。如果用户在太平洋时间,我相信他们会早2小时。

What I am trying to figure out is if there is a way to either

我想弄清楚的是,是否还有办法

  1. tell the server to show me local time when you do a GetDate() on SQL or Now() in ASP
  2. 当您在SQL上执行GetDate()或在ASP中执行Now()时,告诉服务器显示本地时间

  3. figure out some way to do an offset that I can run when the page first loads and use as needed.
  4. 找出一些方法来做一个偏移,我可以在页面首次加载并根据需要使用时运行。

I tried server side javascript, it returns Central Time too. Any help would be greatly appreciated!! Thanks in advance!

我试过服务器端的javascript,它也返回*时间。任何帮助将不胜感激!!提前致谢!

Dennis


UPDATE - 4/11/12 @ 1:12pm:

更新 - 4/11/12 @ 1:12 pm:


I think that I found a work around for my application, but it would not work generically. I have geographic data for the location I am working with - zip code. I can grab the timezone from that - it would not fully work right for users in other timezones looking at the location, but it does not matter for my app since I just need to be focused on the end time for that location.

我认为我找到了一个适用于我的应用程序的工作,但它通常不会起作用。我有我正在使用的位置的地理数据 - 邮政编码。我可以从中获取时区 - 它不能完全适用于查看该位置的其他时区的用户,但这对我的应用程序无关紧要,因为我只需要关注该位置的结束时间。

This is the other other way(s) I found were provided by JohnB below (specifically #4). thanks everyone. http://www.webmasterworld.com/forum47/600.htm (bottom)

这是我发现的其他方式由JohnB提供(特别是#4)。感谢大家。 http://www.webmasterworld.com/forum47/600.htm(下)


5 个解决方案

#1


3  

EDIT

I tried server side javascript, it returns Central Time too.

我试过服务器端的javascript,它也返回*时间。

Did you mean to say client side JavaScript? You definitely need to use client side script to get the user's device time (not server side script).

你的意思是说客户端JavaScript吗?您肯定需要使用客户端脚本来获取用户的设备时间(而不是服务器端脚本)。


You should read this:

你应该读这个:

Primer on dealing with multiple time zones:

处理多个时区的入门书:

1) Make sure your database server is set to the correct Date/Time in its time zone. Properly account for Daylight Savings Time in its location. Set the server to do this automatically.

1)确保您的数据库服务器在其时区中设置为正确的日期/时间。正确考虑其所在位置的夏令时。将服务器设置为自动执行此操作。

2) Create a table in your database with time zones and their offset from UTC (GMT).

2)在数据库中创建一个表,其中包含时区及其与UTC(GMT)的偏移量。

3) Always store Now() Date/Time in UTC. Every database vendor should have a UTC Date/Time Now() function (i.e. SYSUTCDATETIME() for SQL Server). This way all times are stored in a universal format agnostic to where the user happens to be sitting. Call Now() from your database, not the client, because mobile devices could be anywhere, but your database server stays in one spot.

3)始终以UTC格式存储Now()日期/时间。每个数据库供应商都应具有UTC Date / Time Now()函数(即SQL Server的SYSUTCDATETIME())。这样,所有时间都以通用格式存储,与用户恰好坐在的位置无关。从您的数据库而不是客户端调用Now(),因为移动设备可以在任何地方,但您的数据库服务器保留在一个位置。

4) Have user input their local time zone and store it in your database.

4)让用户输入他们的本地时区并将其存储在您的数据库中。

5) When displaying Date/Time stored in UTC back to the user, convert the UTC Date/Time back to the user's time zone using the user's time zone offset. SQL Server makes this a little easier with datetimeoffset.

5)当以UTC格式显示日期/时间返回给用户时,使用用户的时区偏移将UTC日期/时间转换回用户的时区。使用datetimeoffset,SQL Server使这更容易一些。

6) If the user is setting an alarm, have them enter the trigger Date/Time in their local time zone. This way the user can change their local time zone if they move. Also, if time zone rules change you can just fix your time zone table (#2) and then the alarm will still trigger correctly. In your code, to test for alarm trigger, convert trigger time to UTC, and then compare against server time in UTC (i.e. SYSUTCDATETIME()).

6)如果用户正在设置闹钟,请让他们在当地时区输入触发日期/时间。这样,用户可以在移动时更改其本地时区。此外,如果时区规则发生变化,您只需修复时区表(#2),然后警报仍会正确触发。在您的代码中,要测试警报触发器,将触发时间转换为UTC,然后与UTC中的服务器时间进行比较(即SYSUTCDATETIME())。

7) Daylight Savings Time is tricky! (see 1st link)

7)夏令时很棘手! (见第1个链接)

#2


2  

In general, Time zone manipulation can't be done directly in classic ASP.

通常,时区操作不能直接在经典ASP中完成。

However, if you have full control of the server where the code is running, you can install a COM component written in a language that does have time zone support, then use that component from your classic ASP environment.

但是,如果您完全控制运行代码的服务器,则可以安装以具有时区支持的语言编写的COM组件,然后使用传统ASP环境中的该组件。

For example, you might write the following component in .NET with C#:

例如,您可以使用C#在.NET中编写以下组件:

using System;
using System.Runtime.InteropServices;

namespace TimeZoneInfoCom
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [Guid("E0C70A94-352D-4C0B-8C2E-8066C88565C5")]
    public class TimeZoneConverter
    {
        public DateTime NowInZone(string timeZoneId)
        {
            return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, timeZoneId);
        }

        public DateTime Convert(DateTime dateTime, string sourceZoneId, string targetZoneId)
        {
            TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(sourceZoneId);
            TimeZoneInfo targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(targetZoneId);
            return TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, targetTimeZone);
        }
    }
}

You would then compile this, copy the DLL to your server, and register it as a COM component (using RegAsm.exe).

然后,您将编译它,将DLL复制到您的服务器,并将其注册为COM组件(使用RegAsm.exe)。

Then you could call it in your Classic ASP page, like so:

然后你可以在你的经典ASP页面中调用它,如下所示:

<html>
<body>

Server Time: <%= Now() %><br>
<br>

<% 
Dim tzconverter
Set tzconverter = Server.CreateObject("TimeZoneInfoCom.TimeZoneConverter")
%>

US Pacific Time:  <%= tzconverter.NowInZone("Pacific Standard Time") %><br>
US Mountain Time:  <%= tzconverter.NowInZone("Mountain Standard Time") %><br>
US Central Time:  <%= tzconverter.NowInZone("Central Standard Time") %><br>
US Eastern Time:  <%= tzconverter.NowInZone("Eastern Standard Time") %><br>
UTC:  <%= tzconverter.NowInZone("UTC") %><br>

<br>

Conversion Example:
<%

Dim originalTime, convertedTime
originalTime = #12/31/2014 00:00:00#
convertedTime = tzconverter.Convert(originalTime, "UTC", "Tokyo Standard Time")
Response.Write(convertedTime)

%>

<%
' Don't forget to destroy the com object!
Set tzconverter = Nothing
%>

</body>
</html>

If you get an "ActiveX component can't create object" error, be sure that you have set "Enable 32-Bit Applications" to True in IIS, under the advanced settings for your application pool.

如果出现“ActiveX组件无法创建对象”错误,请确保在应用程序池的高级设置下,在IIS中将“启用32位应用程序”设置为True。

With regard to SQL Server - If you search, you may find a handful of posts showing ways you can manipulate time in SQL Server, through elaborate stored procedures that either have fixed offsets, fixed time zone rules, or rely on tables of time zone data. I usually advise against any of these approaches because they are too brittle.

关于SQL Server - 如果你搜索,你可能会发现一些帖子显示你可以在SQL Server中操作时间的方法,通过精心设计的存储过程,它们有固定的偏移量,固定的时区规则,或者依赖于时区数据表。我通常建议不要采用任何这些方法,因为它们太脆弱了。

  • Fixed offsets are bad because they don't account for daylight saving time.
  • 固定偏移是不好的,因为它们不考虑夏令时。

  • Fixed rules are bad because time zone rules can (and do) change. Editing stored procs to keep up with these changes is too fragile (IMHO).
  • 固定规则很糟糕,因为时区规则可以(并且确实)发生变化。编辑存储过程以跟上这些变化太脆弱了(恕我直言)。

  • Maintaining tables of time zone data is a little better, but usually I find these tables to not be maintained well. If you go down this route, be sure to put a procedure in place for updating the tables periodically.
  • 维护时区数据表略好一些,但通常我发现这些表不能很好地维护。如果沿着这条路走下去,请务必设置一个程序来定期更新表格。

#3


0  

Getting the users time zone

http://www.pageloom.com/automatic-timezone-detection-with-javascript

From what I understand about this JavaScript code it is very accurate and will be able to return the offset from UST, the corresponding Olson Database timezone name, and handle the daylight savings time issue (ex. -5:00, America/New_york, true).

根据我对这个JavaScript代码的理解,它非常准确,并且能够从UST返回偏移量,相应的Olson数据库时区名称,并处理夏令时问题(例如-5:00,America / New_york,true )。

The only hurdle you will face after getting this code working on your html page will likely be getting these values to asp and then to sql if that is what you need. I achieved this by sending these values as a $.post using JQuery. I think this is the easiest way to do it. I believe the other alternatives are using an AJAX command or cookies.

在将此代码用于html页面后,您将面临的唯一障碍可能是将这些值传递给asp,然后再获取sql,如果这是您需要的。我通过使用JQuery将这些值作为$ .post发送来实现这一点。我认为这是最简单的方法。我相信其他选择使用AJAX命令或cookie。

After I got the values from the JavaScript code to the server I stored them as session variables so they would change if the user on my site logged in from a different timezone then usual. However they could easily be saved to the database as well.

在我从JavaScript代码到服务器的值之后,我将它们存储为会话变量,这样如果我站点上的用户从通常的不同时区登录,它们就会改变。但是,它们也可以很容易地保存到数据库中。

//replace this comment with the most updated timezonedetect code from that first link
var timezone = jstz.determine_timezone();
var tzoffset = timezone.offset();
var tzname = timezone.name();
var tzdst = timezone.dst();
$.post("tzdetect.asp", { tzoffset: tzoffset, tzname: tzname, tzdst: tzdst } );

Then you need to set up the receiving file tzdetect.asp on the server to store the time zone once it's sent.

然后,您需要在服务器上设置接收文件tzdetect.asp,以便在发送时存储时区。

Working with the time zone once you have it

This article has a good solution to your problem: http://www.codeproject.com/Articles/31146/SQL-2005-Time-Zone-Conversion-Functions

本文为您的问题提供了一个很好的解决方案:http://www.codeproject.com/Articles/31146/SQL-2005-Time-Zone-Conversion-Functions

Approach is to set up a scalar function called NEW_TIME that takes three parameters:

方法是设置一个名为NEW_TIME的标量函数,该函数有三个参数:

  • date to convert
  • 转换日期

  • original time zone value
  • 原始时区价值

  • conversion time zone
  • 转换时区

and the function returns the converted value.

并且该函数返回转换后的值。

#4


0  

New solution to an old question...

旧问题的新解决方案......

We just had this issue when moving a server from "eastern standard time" to "utc", with all our asp classic apps.
But, we're using SQL server 2017.

我们在使用所有asp经典应用程序将服务器从“东部标准时间”移动到“utc”时遇到了这个问题。但是,我们正在使用SQL Server 2017。

And, if you're using SQL server 2016+, you can use the new "at time zone" keyword.

而且,如果您使用的是SQL Server 2016+,则可以使用新的“at time zone”关键字。

Get your current "offset" from that query:

从该查询中获取当前的“偏移量”:

select datediff(hour, GETUTCDATE() at time zone 'eastern standard time', GETUTCDATE()) as offset

Then, use that offset to handle all your dates in your ASP code, with functions like:

然后,使用该偏移量来处理ASP代码中的所有日期,其功能如下:

function fromUTC(dt)
    fromUTC = dateadd("h", tzOFFSET, dt)
end function

function toUTC(dt)
    toUTC = dateadd("h", -1 * tzOFFSET, dt)
end function

function getNow
    getNow = fromUTC(now)
end function

You can replace all "now" with "getNow" in your code.

您可以在代码中将所有“now”替换为“getNow”。

Warning: that this offset is fixed according to the current date. If you need to handle dates from different daylight period, you can use the "at time zone" syntax directly in your SQL query, like:

警告:此偏移量根据当前日期确定。如果您需要处理来自不同日光周期的日期,可以直接在SQL查询中使用“at time zone”语法,例如:

DECLARE @timezone NVARCHAR(100) = N'eastern standard time'
SELECT 
    u.name, 
    u.last_visit   at time zone @timezone as last_visit_tz,
    u.date_created at time zone @timezone as date_created_tz
FROM users u

#5


-2  

I think if you choose from below the one you need and then put at the top of your page it will default to that location instead of the server - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

我想如果你从你需要的那个下面选择,然后把它放在你的页面的顶部它将默认为该位置而不是服务器 - http://msdn.microsoft.com/en-us/library/ms524330(v = VS.90)的.aspx

' This file does not need @LCID or @CODEPAGE and 
'  it does not need to be saved in UTF-8 format because  
'  there are no literal strings that need formatting or encoding. 

Response.Codepage = 65001 
Response.Charset = "utf-8" 

' See what happens when you uncomment the lines below. 
'Response.Codepage = 1252 
'Response.Charset = "windows-1252" 

ShowDateTimeCurrency 1033, "North America" 
ShowDateTimeCurrency 1041, "Japan" 
ShowDateTimeCurrency 1049, "Russia" 
ShowDateTimeCurrency 1031, "Germany" 
ShowDateTimeCurrency 1025, "Saudi Arabia" 
ShowDateTimeCurrency 1081, "India" 
ShowDateTimeCurrency 2052, "China" 
ShowDateTimeCurrency 1042, "Korea" 

Sub ShowDateTimeCurrency(iLCID, sLocale) 
  Response.LCID = iLCID 
  Response.Write "<B>" & sLocale & "</B><BR>" 
  Response.Write FormatDateTime(Date, 1) & "<BR>" 
  Response.Write FormatDateTime(Time, 3) & "<BR>" 
  Response.Write FormatCurrency(1000) & "<BR>" 
  Response.Write FormatNumber(50, 3, 0, 0, -1) & " & " & FormatNumber(.02, 3, 0, 0, -1) & "<BR><BR>" 
End Sub

Have a look here - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

看看这里 - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

or another way would be to use date addadd function for time

或者另一种方法是使用date addadd函数来表示时间

Please Mark as answer if it helps thanks

如果感谢有帮助,请标记为答案

#1


3  

EDIT

I tried server side javascript, it returns Central Time too.

我试过服务器端的javascript,它也返回*时间。

Did you mean to say client side JavaScript? You definitely need to use client side script to get the user's device time (not server side script).

你的意思是说客户端JavaScript吗?您肯定需要使用客户端脚本来获取用户的设备时间(而不是服务器端脚本)。


You should read this:

你应该读这个:

Primer on dealing with multiple time zones:

处理多个时区的入门书:

1) Make sure your database server is set to the correct Date/Time in its time zone. Properly account for Daylight Savings Time in its location. Set the server to do this automatically.

1)确保您的数据库服务器在其时区中设置为正确的日期/时间。正确考虑其所在位置的夏令时。将服务器设置为自动执行此操作。

2) Create a table in your database with time zones and their offset from UTC (GMT).

2)在数据库中创建一个表,其中包含时区及其与UTC(GMT)的偏移量。

3) Always store Now() Date/Time in UTC. Every database vendor should have a UTC Date/Time Now() function (i.e. SYSUTCDATETIME() for SQL Server). This way all times are stored in a universal format agnostic to where the user happens to be sitting. Call Now() from your database, not the client, because mobile devices could be anywhere, but your database server stays in one spot.

3)始终以UTC格式存储Now()日期/时间。每个数据库供应商都应具有UTC Date / Time Now()函数(即SQL Server的SYSUTCDATETIME())。这样,所有时间都以通用格式存储,与用户恰好坐在的位置无关。从您的数据库而不是客户端调用Now(),因为移动设备可以在任何地方,但您的数据库服务器保留在一个位置。

4) Have user input their local time zone and store it in your database.

4)让用户输入他们的本地时区并将其存储在您的数据库中。

5) When displaying Date/Time stored in UTC back to the user, convert the UTC Date/Time back to the user's time zone using the user's time zone offset. SQL Server makes this a little easier with datetimeoffset.

5)当以UTC格式显示日期/时间返回给用户时,使用用户的时区偏移将UTC日期/时间转换回用户的时区。使用datetimeoffset,SQL Server使这更容易一些。

6) If the user is setting an alarm, have them enter the trigger Date/Time in their local time zone. This way the user can change their local time zone if they move. Also, if time zone rules change you can just fix your time zone table (#2) and then the alarm will still trigger correctly. In your code, to test for alarm trigger, convert trigger time to UTC, and then compare against server time in UTC (i.e. SYSUTCDATETIME()).

6)如果用户正在设置闹钟,请让他们在当地时区输入触发日期/时间。这样,用户可以在移动时更改其本地时区。此外,如果时区规则发生变化,您只需修复时区表(#2),然后警报仍会正确触发。在您的代码中,要测试警报触发器,将触发时间转换为UTC,然后与UTC中的服务器时间进行比较(即SYSUTCDATETIME())。

7) Daylight Savings Time is tricky! (see 1st link)

7)夏令时很棘手! (见第1个链接)

#2


2  

In general, Time zone manipulation can't be done directly in classic ASP.

通常,时区操作不能直接在经典ASP中完成。

However, if you have full control of the server where the code is running, you can install a COM component written in a language that does have time zone support, then use that component from your classic ASP environment.

但是,如果您完全控制运行代码的服务器,则可以安装以具有时区支持的语言编写的COM组件,然后使用传统ASP环境中的该组件。

For example, you might write the following component in .NET with C#:

例如,您可以使用C#在.NET中编写以下组件:

using System;
using System.Runtime.InteropServices;

namespace TimeZoneInfoCom
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [Guid("E0C70A94-352D-4C0B-8C2E-8066C88565C5")]
    public class TimeZoneConverter
    {
        public DateTime NowInZone(string timeZoneId)
        {
            return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, timeZoneId);
        }

        public DateTime Convert(DateTime dateTime, string sourceZoneId, string targetZoneId)
        {
            TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(sourceZoneId);
            TimeZoneInfo targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(targetZoneId);
            return TimeZoneInfo.ConvertTime(dateTime, sourceTimeZone, targetTimeZone);
        }
    }
}

You would then compile this, copy the DLL to your server, and register it as a COM component (using RegAsm.exe).

然后,您将编译它,将DLL复制到您的服务器,并将其注册为COM组件(使用RegAsm.exe)。

Then you could call it in your Classic ASP page, like so:

然后你可以在你的经典ASP页面中调用它,如下所示:

<html>
<body>

Server Time: <%= Now() %><br>
<br>

<% 
Dim tzconverter
Set tzconverter = Server.CreateObject("TimeZoneInfoCom.TimeZoneConverter")
%>

US Pacific Time:  <%= tzconverter.NowInZone("Pacific Standard Time") %><br>
US Mountain Time:  <%= tzconverter.NowInZone("Mountain Standard Time") %><br>
US Central Time:  <%= tzconverter.NowInZone("Central Standard Time") %><br>
US Eastern Time:  <%= tzconverter.NowInZone("Eastern Standard Time") %><br>
UTC:  <%= tzconverter.NowInZone("UTC") %><br>

<br>

Conversion Example:
<%

Dim originalTime, convertedTime
originalTime = #12/31/2014 00:00:00#
convertedTime = tzconverter.Convert(originalTime, "UTC", "Tokyo Standard Time")
Response.Write(convertedTime)

%>

<%
' Don't forget to destroy the com object!
Set tzconverter = Nothing
%>

</body>
</html>

If you get an "ActiveX component can't create object" error, be sure that you have set "Enable 32-Bit Applications" to True in IIS, under the advanced settings for your application pool.

如果出现“ActiveX组件无法创建对象”错误,请确保在应用程序池的高级设置下,在IIS中将“启用32位应用程序”设置为True。

With regard to SQL Server - If you search, you may find a handful of posts showing ways you can manipulate time in SQL Server, through elaborate stored procedures that either have fixed offsets, fixed time zone rules, or rely on tables of time zone data. I usually advise against any of these approaches because they are too brittle.

关于SQL Server - 如果你搜索,你可能会发现一些帖子显示你可以在SQL Server中操作时间的方法,通过精心设计的存储过程,它们有固定的偏移量,固定的时区规则,或者依赖于时区数据表。我通常建议不要采用任何这些方法,因为它们太脆弱了。

  • Fixed offsets are bad because they don't account for daylight saving time.
  • 固定偏移是不好的,因为它们不考虑夏令时。

  • Fixed rules are bad because time zone rules can (and do) change. Editing stored procs to keep up with these changes is too fragile (IMHO).
  • 固定规则很糟糕,因为时区规则可以(并且确实)发生变化。编辑存储过程以跟上这些变化太脆弱了(恕我直言)。

  • Maintaining tables of time zone data is a little better, but usually I find these tables to not be maintained well. If you go down this route, be sure to put a procedure in place for updating the tables periodically.
  • 维护时区数据表略好一些,但通常我发现这些表不能很好地维护。如果沿着这条路走下去,请务必设置一个程序来定期更新表格。

#3


0  

Getting the users time zone

http://www.pageloom.com/automatic-timezone-detection-with-javascript

From what I understand about this JavaScript code it is very accurate and will be able to return the offset from UST, the corresponding Olson Database timezone name, and handle the daylight savings time issue (ex. -5:00, America/New_york, true).

根据我对这个JavaScript代码的理解,它非常准确,并且能够从UST返回偏移量,相应的Olson数据库时区名称,并处理夏令时问题(例如-5:00,America / New_york,true )。

The only hurdle you will face after getting this code working on your html page will likely be getting these values to asp and then to sql if that is what you need. I achieved this by sending these values as a $.post using JQuery. I think this is the easiest way to do it. I believe the other alternatives are using an AJAX command or cookies.

在将此代码用于html页面后,您将面临的唯一障碍可能是将这些值传递给asp,然后再获取sql,如果这是您需要的。我通过使用JQuery将这些值作为$ .post发送来实现这一点。我认为这是最简单的方法。我相信其他选择使用AJAX命令或cookie。

After I got the values from the JavaScript code to the server I stored them as session variables so they would change if the user on my site logged in from a different timezone then usual. However they could easily be saved to the database as well.

在我从JavaScript代码到服务器的值之后,我将它们存储为会话变量,这样如果我站点上的用户从通常的不同时区登录,它们就会改变。但是,它们也可以很容易地保存到数据库中。

//replace this comment with the most updated timezonedetect code from that first link
var timezone = jstz.determine_timezone();
var tzoffset = timezone.offset();
var tzname = timezone.name();
var tzdst = timezone.dst();
$.post("tzdetect.asp", { tzoffset: tzoffset, tzname: tzname, tzdst: tzdst } );

Then you need to set up the receiving file tzdetect.asp on the server to store the time zone once it's sent.

然后,您需要在服务器上设置接收文件tzdetect.asp,以便在发送时存储时区。

Working with the time zone once you have it

This article has a good solution to your problem: http://www.codeproject.com/Articles/31146/SQL-2005-Time-Zone-Conversion-Functions

本文为您的问题提供了一个很好的解决方案:http://www.codeproject.com/Articles/31146/SQL-2005-Time-Zone-Conversion-Functions

Approach is to set up a scalar function called NEW_TIME that takes three parameters:

方法是设置一个名为NEW_TIME的标量函数,该函数有三个参数:

  • date to convert
  • 转换日期

  • original time zone value
  • 原始时区价值

  • conversion time zone
  • 转换时区

and the function returns the converted value.

并且该函数返回转换后的值。

#4


0  

New solution to an old question...

旧问题的新解决方案......

We just had this issue when moving a server from "eastern standard time" to "utc", with all our asp classic apps.
But, we're using SQL server 2017.

我们在使用所有asp经典应用程序将服务器从“东部标准时间”移动到“utc”时遇到了这个问题。但是,我们正在使用SQL Server 2017。

And, if you're using SQL server 2016+, you can use the new "at time zone" keyword.

而且,如果您使用的是SQL Server 2016+,则可以使用新的“at time zone”关键字。

Get your current "offset" from that query:

从该查询中获取当前的“偏移量”:

select datediff(hour, GETUTCDATE() at time zone 'eastern standard time', GETUTCDATE()) as offset

Then, use that offset to handle all your dates in your ASP code, with functions like:

然后,使用该偏移量来处理ASP代码中的所有日期,其功能如下:

function fromUTC(dt)
    fromUTC = dateadd("h", tzOFFSET, dt)
end function

function toUTC(dt)
    toUTC = dateadd("h", -1 * tzOFFSET, dt)
end function

function getNow
    getNow = fromUTC(now)
end function

You can replace all "now" with "getNow" in your code.

您可以在代码中将所有“now”替换为“getNow”。

Warning: that this offset is fixed according to the current date. If you need to handle dates from different daylight period, you can use the "at time zone" syntax directly in your SQL query, like:

警告:此偏移量根据当前日期确定。如果您需要处理来自不同日光周期的日期,可以直接在SQL查询中使用“at time zone”语法,例如:

DECLARE @timezone NVARCHAR(100) = N'eastern standard time'
SELECT 
    u.name, 
    u.last_visit   at time zone @timezone as last_visit_tz,
    u.date_created at time zone @timezone as date_created_tz
FROM users u

#5


-2  

I think if you choose from below the one you need and then put at the top of your page it will default to that location instead of the server - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

我想如果你从你需要的那个下面选择,然后把它放在你的页面的顶部它将默认为该位置而不是服务器 - http://msdn.microsoft.com/en-us/library/ms524330(v = VS.90)的.aspx

' This file does not need @LCID or @CODEPAGE and 
'  it does not need to be saved in UTF-8 format because  
'  there are no literal strings that need formatting or encoding. 

Response.Codepage = 65001 
Response.Charset = "utf-8" 

' See what happens when you uncomment the lines below. 
'Response.Codepage = 1252 
'Response.Charset = "windows-1252" 

ShowDateTimeCurrency 1033, "North America" 
ShowDateTimeCurrency 1041, "Japan" 
ShowDateTimeCurrency 1049, "Russia" 
ShowDateTimeCurrency 1031, "Germany" 
ShowDateTimeCurrency 1025, "Saudi Arabia" 
ShowDateTimeCurrency 1081, "India" 
ShowDateTimeCurrency 2052, "China" 
ShowDateTimeCurrency 1042, "Korea" 

Sub ShowDateTimeCurrency(iLCID, sLocale) 
  Response.LCID = iLCID 
  Response.Write "<B>" & sLocale & "</B><BR>" 
  Response.Write FormatDateTime(Date, 1) & "<BR>" 
  Response.Write FormatDateTime(Time, 3) & "<BR>" 
  Response.Write FormatCurrency(1000) & "<BR>" 
  Response.Write FormatNumber(50, 3, 0, 0, -1) & " & " & FormatNumber(.02, 3, 0, 0, -1) & "<BR><BR>" 
End Sub

Have a look here - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

看看这里 - http://msdn.microsoft.com/en-us/library/ms524330(v=vs.90).aspx

or another way would be to use date addadd function for time

或者另一种方法是使用date addadd函数来表示时间

Please Mark as answer if it helps thanks

如果感谢有帮助,请标记为答案