131

The statement gives me the date and time.

How could I modify the statement so that it returns only the date (and not the time)?

SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint ) / 1000 ) );

6 Answers 6

223

You use to_timestamp function and then cast the timestamp to date

 select to_timestamp(epoch_column)::date;

You can use more standard cast instead of ::

select cast(to_timestamp(epoch_column) as date);

More details:

/* Current time */
 select now();  -- returns timestamp

/* Epoch from current time;
   Epoch is number of seconds since 1970-01-01 00:00:00+00 */
 select extract(epoch from now()); 

/* Get back time from epoch */
 -- Option 1 - use to_timestamp function
 select to_timestamp( extract(epoch from now()));
 -- Option 2 - add seconds to 'epoch'
 select timestamp with time zone 'epoch' 
         + extract(epoch from now()) * interval '1 second';

/* Cast timestamp to date */
 -- Based on Option 1
 select to_timestamp(extract(epoch from now()))::date;
 -- Based on Option 2
 select (timestamp with time zone 'epoch' 
          + extract(epoch from now()) * interval '1 second')::date; 

In your case:

 select to_timestamp(epoch_ms / 1000)::date;

PostgreSQL Docs

4
  • 1
    doesn't seem to work, i get syntax error. Did it change as of 2018?
    – cryanbhu
    Sep 15, 2018 at 15:34
  • 3
    well i ran the select to_timestamp(extract(epoch epoch_ms))::date; verbatim and it gave a syntax error near epoch_ms. I went to find other solutions and eventually this worked for me SELECT TIMESTAMP 'epoch' + (start_dt) * INTERVAL '1 second' as started_on. Could you please explain the difference between TIMESTAMP and to_timestamp()?
    – cryanbhu
    Sep 16, 2018 at 4:58
  • 3
    TIMESTAMP is just column type where to_timestamp is a build in function that translates unix epoch to timestamp starting calculations from '1970-01-01 00:00:00+00'
    – Losbaltica
    Dec 14, 2018 at 14:15
  • @cryanbhu, your error was caused by not typing from between epoch and epoch_ms; i.e., you should have typed epoch from epoch_ms inside the extract function. May 25, 2023 at 18:02
31
select to_timestamp(cast(epoch_ms/1000 as bigint))::date

worked for me

1
  • any idea how to make this work for older version (Postgres 8.0 / Redshift)?
    – Luc
    Sep 27, 2021 at 9:26
10

On Postgres 10:

SELECT to_timestamp(CAST(epoch_ms as bigint)/1000)

4

The solution above not working for the latest version on PostgreSQL. I found this way to convert epoch time being stored in number and int column type is on PostgreSQL 13:

SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;

For more detail explanation, you can see here https://www.yodiw.com/convert-epoch-time-to-timestamp-in-postgresql/#more-214

1
  • The above solutions will most definitely work with Postgres 12 or 13. But you can simplify your solution to make_timestamp(sec => the_column)
    – user330315
    May 27, 2020 at 17:28
3

This works for me fine:

SELECT t.*,
   to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
   to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
   to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
FROM public.qrtz_triggers t;
2

Seconds since epoch with GNU date:

$ date +%s.%N
1627059870.945134901

This works with PostgreSQL 11:

# select to_timestamp (1627059870.945134901);
         to_timestamp          
-------------------------------
 2021-07-23 19:04:30.945135+02
(1 row)

# select to_timestamp (1627059870.945134901)::date;
 to_timestamp 
--------------
 2021-07-23
(1 row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.