我有一个日期时间变量,其值为
2024-08-12T18:30:00
,采用 UTC 格式。将此值插入 SQL Server 时,我想确保它保留 UTC 名称。具体来说,我希望将该值存储为 2024-08-12T18:30:00.000Z
(其中“Z”表示 UTC),或者使用像 2024-08-12 18:30:00.0000000 +00:00
这样的显式偏移量。
在前端,我使用 JavaScript 的 new Date() 函数,该函数根据“Z”或偏移量的存在自动将日期转换为本地时间。
我正在使用 INSERT INTO 函数,如何确保日期以“Z”或 UTC 偏移量保存在 SQL Server 中,以便它正确指示 UTC 时间?
DATETIMEOFFSET 数据类型处理时区:
Create Table tbl (id INT, datetime_dt DATETIME, dt_offset_dt DATETIMEOFFSET );
Insert Into tbl
Select 1, '2024-08.31 12:00:00', '2024-08-31 12:00:00 +00:00' Union All
Select 2, '2024-08.31 12:00:00', '2024-08-31 12:00:00.0000000 +02:00' ;
Select * From tbl
id datetime_dt dt_offset_dt
-- ----------------------- ----------------------------------
1 2024-08-31 12:00:00.000 2024-08-31 12:00:00.0000000 +00:00
2 2024-08-31 12:00:00.000 2024-08-31 12:00:00.0000000 +02:00