Getdate() 函数获取我的时区的日期 [英] Getdate() function to get date for my timezone

查看:40
本文介绍了Getdate() 函数获取我的时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据类型为 datetime2(7) 的列中插入一个默认值.但是,由于我的网站托管在不同时区的服务器上,因此 getdate 函数无法正常工作.我想知道是否有解决方案.我做了一些研究,发现了两种方法.首先是使用 GetUTCDate() 函数.但是,当我显示信息时,我需要进行转换.我确定我的 Web 应用程序仅用于我的时区.所以我想避免这种情况.第二种方式,这是我使用 SwitchOffSet 函数最接近的方法:

I would love to insert a default value into a column with data type datetime2(7). However, because my website is hosted on a server in a different timezone, the getdate function doesn't work properly. I wonder if there is a solution to this. I have done some research and found two ways. First is to use GetUTCDate() function. However, I would need to do the conversion when I display the information. I am sure my web application is used for only my timezone. So I would like to avoid this. Second way, this is the closest I could get this done by using SwitchOffSet function:

CREATE TABLE [dbo].[Test_Date](
[test_id] [int] NOT NULL,
[test_date] [datetime2](7) NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Test_Date] ADD  CONSTRAINT [DF_Test_Date_test_date]  DEFAULT (switchoffset(CONVERT([datetimeoffset],getutcdate()),'+13:00')) FOR [test_date]
GO

但是,我的问题是接下来几个月的 +13:00 原因,将是夏令时更改的 +12:00 原因.因此,我每次都需要更改它.有人有解决办法吗?

However, my problem is the +13:00 cause in the next few months, it will be +12:00 cause of the day light saving time change. As a result, I would need to change it every time. Anybody has a solution to this?

谢谢.

推荐答案

只需将数据存储在 UTC 中,使用日历表 计算读取数据时的偏移量(请参阅这些提示:第 1 部分第 2 部分, 第 3 部分).相关:

Just store the data in UTC, and use a calendar table to calculate offsets when you read the data (see these tips: part 1, part 2, part 3). Related:

获取过去日期的 UTC 日期

夏令时和时区最佳做法

使用 Datetimeoffset 的最佳实践

如何将带有数据的 sql server 2005 express 数据库中的所有日期时间列转换为 UTC

在 n 层应用程序中的何处设置 UTC 日期时间值:表示层、域或数据库?

我如何处理每个时区聊天消息

这篇关于Getdate() 函数获取我的时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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