Getdate() function to get date for my timezone

做~自己de王妃 提交于 2019-11-30 21:36:46

You can use SYSDATETIMEOFFSET function

select SYSDATETIMEOFFSET()

MSDN description:

Returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.

More on MSDN.


Based on clarification in the comment below:

Because you want to store the local time of the client, SQL Server has no way of knowing what is your local time. The best option that would work best would be to send the current time from the client each time.

Since Sql Server 2016 you can use AT TIME ZONE...

SELECT CONVERT(datetime2(0), '2015-03-29T01:01:00', 126)     
AT TIME ZONE 'Central European Standard Time';  

... as specified in the documentation

SELECT GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'

You need the first 'AT TIME ZONE UTC' in order to tell the DB what the value currently is in, so it knows how to get to the second given time zone, 'Central Standard Time' in my example.

since the db timezone info is different with your web server, its best you explicitly pass your desired datetime value from your web app to the db, instead of using db server-side default function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!