将getdate()转换为EST

时间:2011-01-17 11:26:04

标签: sql sql-server tsql datetime getdate

我想将SQL Server中的getdate()转换为EST时间。

13 个答案:

答案 0 :(得分:8)

您是否考虑过GETUTCDATE()并从那里执行偏移? 如果你的标准时间是EST,那就是UTC-5,所以

select dateadd(hour,-5,GETUTCDATE())

答案 1 :(得分:8)

这些都是错误的答案

EST到UTC并不总是5小时的差异。这取决于节省时间 它可以是4小时或5小时。 http://ww2010.atmos.uiuc.edu/(Gh)/guides/maps/utc/frutc.rxml

答案 2 :(得分:2)

您也可以尝试

select SWITCHOFFSET ( SYSDATETIMEOFFSET(), '-05:00')

答案 3 :(得分:2)

SQL服务器本身的表current_utc_offset在夏季和冬季时具有正确的偏移量。 请尝试查询select * from current_utc_offset,将日期更改为服务器上的其他季节,然后再次查看表格。 因此获得EST的正确功能是:

CREATE FUNCTION [dbo].[Getestlocaldatetime] () 
returns DATETIME 
AS 
  BEGIN 
      DECLARE @zz NVARCHAR(12); 
      DECLARE @hh NVARCHAR(3); 
      DECLARE @dd DATETIME; 

      SET @zz = (SELECT current_utc_offset 
                 FROM   sys.time_zone_info 
                 WHERE  NAME = N'US Eastern Standard Time') 
      SET @hh = Substring(@zz, 1, 3); 
      SET @dd = Dateadd(hh, CONVERT(INT, @hh), Getutcdate()) 

      RETURN @dd 
  END 

答案 4 :(得分:1)

EST是GMT-5小时,而EDT是GMT-4小时。

获得EST:

select dateadd(hour,-5,GETUTCDATE())

获得EDT:

select dateadd(hour,-4,GETUTCDATE())

答案 5 :(得分:0)

GetDate()是服务器本身的系统时间。

现在考虑GetDate()的小时差和它现在在EST的时间使用此代码,其中1是所述差异(在这种情况下服务器位于中心时区)(这也是假设你的服务器占DST)

SELECT Dateadd(hour, 1, Getdate()) AS EST 

答案 6 :(得分:0)

使用GETDATE():

GMT:

日期将会显示:

SELECT GETDATE()

image1

EST:

现在使用GETDATE()进行转换:

SELECT DATEADD(HOUR, -4, CONVERT(varchar(20),GETDATE(),120))

image2

答案 7 :(得分:0)

如果您尝试使用夏令时输出当地时间(例如东部时间),则需要一个功能来检测夏令时的开始和结束,然后应用变量偏移:我发现了这个: http://joeyiodice.com/convert-sql-azure-getdate-utc-time-to-local-time/很有用。

答案 8 :(得分:0)

对于使用最新版本sql server的用户,可以创建.net函数

标量值函数(SVF)返回单个值,例如字符串,整数或位值。您可以使用任何.NET Framework编程语言在托管代码中创建标量值用户定义的函数。这些功能可供Transact-SQL或其他托管代码访问。有关CLR集成的优点以及在托管代码和Transact-SQL之间进行选择的信息。

由于.NET可以访问操作系统中的所有时区,因此您不必计算夏时制-4或-5基础。

var timeUtc = DateTime.UtcNow;
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

答案 9 :(得分:0)

SELECT CONVERT(VARCHAR, CONVERT(DATETIME, GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time'), 100) AS [Date_Result]

https://stackoverflow.com/a/49449695/6559330

答案 10 :(得分:0)

如果要执行此操作而不调用函数,则也可以使用CASE语句来执行。下面的代码将UTC字段转换为Mountain Time,以节省夏令时。对于EST,您只需将所有-6更改为-4,并将所有-7更改为-5。

--Adjust a UTC value, in the example the UTC field is identified as UTC.Field, to account for daylight savings time when converting out of UTC to Mountain time.
CASE
    --When it's between March and November, it is summer time which is -6 from UTC
    WHEN MONTH ( UTC.Field ) > 3 AND MONTH ( UTC.Field ) < 11 
        THEN DATEADD ( HOUR , -6 , UTC.Field )
    --When its March and the day is greater than the 14, you know it's summer (-6)
    WHEN MONTH ( UTC.Field ) = 3
        AND DATEPART ( DAY , UTC.Field ) >= 14 
        THEN
            --However, if UTC is before 9am on that Sunday, then it's before 2am Mountain which means it's still Winter daylight time.
            CASE 
                WHEN DATEPART ( WEEKDAY , UTC.Field ) = 1 
                    AND UTC.Field < '9:00'
                    --Before 2am mountain time so it's winter, -7 hours for Winter daylight time
                    THEN DATEADD ( HOUR , -7 , UTC.Field )
                --Otherwise -6 because it'll be after 2am making it Summer daylight time
                ELSE DATEADD ( HOUR , -6 , UTC.Field )
            END
    WHEN MONTH ( UTC.Field ) = 3
        AND ( DATEPART ( WEEKDAY , UTC.Field ) + 7 ) <= DATEPART ( day , UTC.Field ) 
        THEN 
            --According to the date, it's moved onto Summer daylight, but we need to account for the hours leading up to 2am if it's Sunday
            CASE 
                WHEN DATEPART ( WEEKDAY , UTC.Field ) = 1 
                    AND UTC.Field < '9:00'
                    --Before 9am UTC is before 2am Mountain so it's winter Daylight, -7 hours
                    THEN DATEADD ( HOUR , -7 , UTC.Field )
                --Otherwise, it's summer daylight, -6 hours
                ELSE DATEADD ( HOUR , -6 , UTC.Field )
            END
    --When it's November and the weekday is greater than the calendar date, it's still Summer so -6 from the time
    WHEN MONTH ( UTC.Field ) = 11
        AND DATEPART ( WEEKDAY , UTC.Field ) > DATEPART ( DAY , UTC.Field ) 
        THEN DATEADD ( HOUR , -6 , UTC.Field )
    WHEN MONTH ( UTC.Field ) = 11
        AND DATEPART ( WEEKDAY , UTC.Field ) <= DATEPART ( DAY , UTC.Field ) 
            --If the weekday is less than or equal to the calendar day it's Winter daylight but we need to account for the hours leading up to 2am.
            CASE 
                WHEN DATEPART ( WEEKDAY , UTC.Field ) = 1 
                    AND UTC.Field < '8:00'
                    --If it's before 8am UTC and it's Sunday in the logic outlined, then it's still Summer daylight, -6 hours
                    THEN DATEADD ( HOUR , -6 , UTC.Field )
                --Otherwise, adjust for Winter daylight at -7
                ELSE DATEADD ( HOUR , -7 , UTC.Field )
            END
    --If the date doesn't fall into any of the above logic, it's Winter daylight, -7
    ELSE
        DATEADD ( HOUR , -7 , UTC.Field )
END

答案 11 :(得分:0)

从SQL Server 2016开始,我们可以使用AT TIME ZONE功能here来在一行SQL中一致地获得EST

SELECT CONVERT(DATETIME,GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time')

以上声明说明了夏令时时间,并且不需要创建功能或过程

说明...上面的查询调用GETDATE(),并使用AT TIMEZONE将其时区设置为UTC。隐式地,这也将其数据类型从datetime更改为datetimeoffset。接下来,我们将再次调用AT TIMEZONE以将其切入EST。最后,我们将整个内容包装在CONVERT()中,以将其恢复为datetime,在此过程中删除了不需要的+/-小时部分。

逐步查询...

SELECT [GetDate]            = GETDATE()
SELECT [GetDateAtUtc]       = GETDATE() AT TIME ZONE 'UTC'
SELECT [GetDateAtUtcAtEst]  = GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time'
SELECT [GetDateEst]         = CONVERT(DATETIME,GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time')

enter image description here

答案 12 :(得分:0)

选择CONVERT(DATETIME,GETUTCDATE()在时区“ UTC”在时区“东部标准时间”)