SQL Server如何在不同的时区中保持和使用时间 [英] SQL Server How to persist and use a time across different time zones
问题描述
在SQL Server中,我想创建一个表来节省事件的时间,并希望将其转换为用户选择的时区以进行显示。让我们说如果在格林尼治标准时间下午1:00在伦敦发生一件事情,那就是美国东部标准时间上午8点。
In SQL Server I would like to create a table to save time of an event, and would like to convert it into the timezone of the users choice for display purposes. Let us say that If there was an event that happens in London at 1:00 PM GMT, that would be 8:00 am US EST.
给出这个例子我会喜欢创建一个框架工作,
Given This example I would like to create a frame work,
-
用户将有能力保存事件和时间(给予时区的事件)
where a user would have an ability to save the event and time (Giving the time zone of the event)
阅读这些事件,时间显示在他喜欢的时区(US EST)
Read Those events, with the time displayed in the time zone of his liking (US EST)
如何在SQL Server中完成此操作。
How do I accomplish this in SQL Server.
推荐答案
p>在SQL Server 2008中,使用 DATETIMEOFFSET 数据类型DATETIME加上时区偏移包括。
In SQL Server 2008, use the DATETIMEOFFSET data type which is a DATETIME plus a timezone offset included.
SELECT CAST('2010-11-23 16:35:29+09:00' AS datetimeoffset)
将在2010年11月23日下午4点35分在+9小时(从GMT )时区。
would be Nov 23, 2010, 4:35pm in a +9 hour (from GMT) timezone.
SQL Server 2008还包含函数和SQL命令将 DATETIMEOFFSET
从一个时区转换为另一个:
SQL Server 2008 also contains functions and SQL commands to convert DATETIMEOFFSET
values from one timezone to another:
SELECT
SWITCHOFFSET(CAST('2010-11-23 16:35:29+09:00' AS datetimeoffset), '+01:00')
将导致:
2010-11-23 08:35:29.0000000 +01:00
同一时间,不同的时区(GMT的+1小时)
Same time, different timezone (+1 hour from GMT)
这篇关于SQL Server如何在不同的时区中保持和使用时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!