In SQL server you can get the local datetime UTC time . Convert it to different TimeZone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DECLARE @CurrentDateTime DATETIME = GETDATE(); DECLARE @CurrentUTC DATETIME = GETUTCDATE(); DECLARE @TimeZoneLocal VARCHAR(50) EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SYSTEM\CurrentControlSet\Control\TimeZoneInformation','TimeZoneKeyName',@TimeZoneLocal OUT SELECT @TimeZoneLocal AS ServerTimeZone, GETDATE() AS ServerCurrentDT, GETUTCDATE() AS 'GetUTCDATE', GETDATE() AT TIME ZONE 'UTC' AS 'UTC', GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' AS 'to EST', GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time' AS 'to PST', GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time' AS 'to CET', GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE'India Standard Time' AS 'to IST'; |
Convert a local datetime or the server local time to different timezone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
DECLARE @OneDatetime DATETIME = '2024-07-05 16:21:00.000'; -- Replace with your datetime value in Local server datetime Set @OneDatetime = GETDATE() -- you can comment out if you have spec datetime for local server in previous line DECLARE @TimeZone VARCHAR(50) EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SYSTEM\CurrentControlSet\Control\TimeZoneInformation','TimeZoneKeyName',@TimeZone OUT SELECT @OneDatetime AS OneDatetime , @OneDatetime AT TIME ZONE @TimeZone AS 'Local_OneDatetime' , Convert(DATETIME, @OneDatetime AT TIME ZONE @TimeZone AT TIME ZONE 'Pacific Standard Time')AS 'to PST', Convert(DATETIME, @OneDatetime AT TIME ZONE @TimeZone AT TIME ZONE 'Eastern Standard Time') AS 'to EST', Convert(DATETIME, @OneDatetime AT TIME ZONE @TimeZone AT TIME ZONE 'UTC' ) AS 'to UTC', Convert(DATETIME, @OneDatetime AT TIME ZONE @TimeZone AT TIME ZONE 'Central European Standard Time') AS 'to CET', Convert(DATETIME, @OneDatetime AT TIME ZONE @TimeZone AT TIME ZONE 'India Standard Time') AS 'to IST', @TimeZone AS ServerTimeZone, GETDATE() AS Server_Currrent_GetDate, @OneDatetime AT TIME ZONE @TimeZone AS 'Server_current_offset' |
Convert UTC datetime or the server UTC time to different timezone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
DECLARE @UTCOneDatetime DATETIME = '2024-07-05 20:21:00'; -- Replace with your datetime value in Local server datetime Set @UTCOneDatetime = GETUTCDATE() -- yo can comment out if you have UTC datetime DECLARE @TimeZone VARCHAR(50) EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SYSTEM\CurrentControlSet\Control\TimeZoneInformation','TimeZoneKeyName',@TimeZone OUT SELECT @UTCOneDatetime AS UTCOneDatetime , --@UTCOneDatetime AT TIME ZONE @TimeZone AS 'UTC_OneDatetime' , Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time')AS 'to PST', Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time') AS 'to EST', Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' ) AS 'to UTC', Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time') AS 'to CET', Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'India Standard Time') AS 'to IST', Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE 'UTC') AS UTC, Convert(DATETIME, @UTCOneDatetime AT TIME ZONE 'UTC' AT TIME ZONE @TimeZone) AS '--> To_LocalServerTime <--', @TimeZone AS ServerTimeZone, GETDATE() AS 'Server_Current_GetDate', GETUTCDATE() AS 'Server_Current_GetUTCDate', GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE @TimeZone AS 'Server_Current_offset' |
Continue reading “DateTime calculator to UTC to Local Server time to a specific TimeZone”