Why should we use GETUTCDATE()
function instead of getdate()
or vice-verse?
What is the purpose of both? Please help me with example.
The difference between GETDATE() and GETUTCDATE() is in timezone, the GETDATE() function returns the current date and time in the local timezone, the timezone where your database server is running, but GETUTCDATE() return the current time and date in UTC (Universal Time Coordinate) or GMT timezone.
Inserting a UTC datetime into a datetimeoffset column If you're using GETUTCDATE() to get the UTC datetime, it won't have the timezone offset appended to it. But if you're inserting into a datetimeoffset column, then it will put the UTC timezone offset for you. It automatically appended the +00:00 timezone offset.
SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.
JavaScript Date getUTCDate() getUTCDate() returns the day of the month (1 to 31) of a date object. getUTCDate() returns the day according to UTC.
GETUTCDATE() represents the current UTC
time (Universal Time Coordinate or Greenwich Mean Time)
nondeterministic
function, Views and expressions that reference this column cannot be indexed.The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which SQL Server is running.
The difference between GETDATE()
and GETUTCDATE()
is time zone number of the SQL Server machine.
For better understanding see the example:
DECLARE @local_time DATETIME; DECLARE @gmt_time DATETIME; SET @local_time = GETDATE(); SET @gmt_time = GETUTCDATE(); SELECT 'Server local time: ' + CONVERT(VARCHAR(40),@local_time); SELECT 'Server GMT time: ' + CONVERT(VARCHAR(40),@gmt_time); SELECT 'Server time zone: ' + CONVERT(VARCHAR(40), DATEDIFF(hour,@gmt_time,@local_time)); GO
OUTPUT:
Server local time: Jun 2 2007 10:06PM Server GMT time: Jun 2 2007 4:21PM Server time zone: 6
GETDATE ()
GETUTCDATE ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With