且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 Sql Server 2008 DateTimeOffset 转换为 DateTime

更新时间:2023-02-22 18:26:22

使用几乎任何样式的转换都会导致 datetime2 值转换为 UTC.
此外,从 datetime2 到 datetimeoffset 的转换只是将偏移量设置为 +00:00,如下所示,因此这是一种从 Datetimeoffset(offset!=0)转换的快速方法> 到 Datetimeoffset(+00:00)

Converting using almost any style will cause the datetime2 value to be converted to UTC.
Also, conversion from datetime2 to datetimeoffset simply sets the offset at +00:00, per the below, so it is a quick way to convert from Datetimeoffset(offset!=0) to Datetimeoffset(+00:00)

declare @createdon datetimeoffset
set @createdon = '2008-12-19 17:30:09.1234567 +11:00'

select CONVERT(datetime2, @createdon, 1)
--Output: 2008-12-19 06:30:09.12

select convert(datetimeoffset,CONVERT(datetime2, @createdon, 1))
--Output: 2008-12-19 06:30:09.1234567 +00:00