1

I have these queries

CREATE TABLE times (
  time TIMESTAMP
);

insert into times values(now());
select * from times;
          time            
---------------------------
2021-01-05 15:25:55.170
(1 row)

What I would like is this timestamp formatted with UTC similar to how java formats UTC timestamps (java.time.Instant.now()) 2021-01-05T15:25:55.170Z.

and what I could do is manually format the string like so

select replace(time::text, ' ', 'T') || 'Z' from times;

but is there a more conventional / cleaner way of achieving this timestamp format?

irregular
  • 1,437
  • 3
  • 20
  • 39
  • 1
    [Don't use `TIMESTAMP` but `TIMESTAMPTZ` for this](https://stackoverflow.com/q/9571392/1048572) – Bergi Jan 05 '21 at 20:59

1 Answers1

0

Example:

select to_char('2021-01-05 15:25:55.170'::timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.MST')|| 'Z';
         ?column?          
---------------------------
 2021-01-05T15:25:55.170TZ

For more information on formatting see Data formatting.

Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28