Skip to content

Instantly share code, notes, and snippets.

@drydevelopment
Last active February 15, 2018 20:12
Show Gist options
  • Save drydevelopment/ebc0bb3235956438efb976bb30446f69 to your computer and use it in GitHub Desktop.
Save drydevelopment/ebc0bb3235956438efb976bb30446f69 to your computer and use it in GitHub Desktop.
PostgreSQL function to output an ISO 8601 formatted timestamp with time zone
/* Tested on PostgreSQL 9.6 */
CREATE OR REPLACE FUNCTION iso_8601_timestamp(datetime TIMESTAMP WITH TIME ZONE)
RETURNS VARCHAR AS $$
BEGIN
RETURN to_char(datetime::TIMESTAMPTZ AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
END;
$$
LANGUAGE PLPGSQL;
/* Example Usage */
SELECT iso_8601_timestamp(now());
-- 2018-02-15T20:07:12Z
SELECT iso_8601_timestamp('2018-02-15');
-- 2018-02-15T05:00:00Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment