This function will get the local time based on the time zone difference from UTC
IF OBJECT_ID(N'[GetLocalDate]') IS NOT NULL DROP FUNCTION [GetLocalDate] GO CREATE FUNCTION [dbo].[GetLocalDate] (@TimezoneDiffInHour FLOAT = - 5) RETURNS DATETIME AS BEGIN RETURN DATEADD(Hh, @TimezoneDiffInHour, GETUTCDATE()) END;
SELECT dbo.GetLocalDate(DEFAULT); -- or SELECT dbo.GetLocalDate(-5);
SELECT dbo.GetLocalDate(+1);
Originally Posted on February 2, 2017
Last Updated on March 26, 2021
Last Updated on March 26, 2021
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.