Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest postgreSQL equivalent to MySQL UTC_DATE() (getting UTC date)?

I am new to postgreSQL and to my surprise there appears to be no single function equivalent to UTC_DATE() from MySQL.

I was able to get desired result using a combination of date & timezone functions but I am wondering what is the fastest way to do this.

I just want to get UTC date part, at 00:00:00 time of current day.

like image 733
Valentin Kuzub Avatar asked Aug 22 '11 05:08

Valentin Kuzub


People also ask

Does Postgres store dates as UTC?

PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in UTC .

How do I get the UTC date in MySQL?

MySQL - UTC_DATE() Function The MYSQL UTC_DATE() is used to get the current UTC date. The resultant value is a string or a numerical value based on the context and, the date returned will be in the 'YYYY-MM-DD' or YYYYMMDD format.

How can I save UTC time in SQL?

If they're all local to you, then here's the offset: SELECT GETDATE() AS CurrentTime, GETUTCDATE() AS UTCTime. SELECT DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), YOUR_DATE);


1 Answers

Use the time zone capability of PostgreSQL:

SELECT 
    CAST(NOW() at time zone 'utc' AS date); 
like image 104
Frank Heikens Avatar answered Nov 07 '22 20:11

Frank Heikens