【问题标题】:How to make gatedate() work for different time zones in ms sql server如何使gatedate()适用于ms sql server中的不同时区
【发布时间】:2014-09-20 12:09:39
【问题描述】:

好的,我的数据库中有一张表,其中包含“Order_Placed_Date”列。此列的默认值设置为 getdate(),并且由于我的数据库配置为 India Standard Time (IST GMT+05:30),因此插入的值列将是印度的当前时间。

现在我有另一个表“Time_Zone”,其中包含“Time_Zone_Code”列,其中包含用户的时区代码。

我想要的是在“Order_Placed_Date”列中,而不是为 IST 插入当前时间,gatedate() 应该为“Time_Zone”表中存储的时区代码插入当前时间。

假设“Time_Zone_Code”列的值为东部标准时间,那么在 Order_Placed_Date 列中插入的时间应该是 EST 的当前时间,而不是 IST。

sql server 2008 SWITCHOFFSET 中有一个函数返回两个时区之间的偏移量,但有点困惑,如何在我的场景中使用它。

【问题讨论】:

    标签: sql-server timezone


    【解决方案1】:

    按照 Matt 的建议,我正在我的 c# 代码中进行日期计算,然后将计算出的值插入 Order_Placed_Date 列中。我写了以下函数

    public string getDateForProvideTimeZone(string TimeZoneId)
    {
                TimeZoneInfo _timeZoneInfo;
                DateTime _dateTime;
    
                _timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId);
                _dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, _timeZoneInfo);
                return _dateTime.ToString("dd-MM-yyyy HH:mm:ss");
    }
    

    【讨论】:

      【解决方案2】:

      SQL Server 没有此功能。请考虑在您的应用程序代码中执行时区调整。

      例如,如果您在应用程序代码中使用 .NET,则可以使用 TimeZoneInfo 类。

      【讨论】:

        猜你喜欢
        • 2010-11-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2012-11-24
        • 2015-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多