开发者

Formatting Postgresql dates with Timezone

开发者 https://www.devze.com 2022-12-18 11:58 出处:网络
How can I output postgresql dates with ISO 8601-compliant timezones? e.g. I have the value 2006-11-10 07:35:30+01

How can I output postgresql dates with ISO 8601-compliant timezones?

e.g. I have the value

2006-11-10 07:35:30+01

and I need it in this format:

2006-11-10T07:35+01

I could do that easily enough with string manipulation, but the standard date formatting 开发者_开发知识库functions for

to_char(<my date>, 'YYYY-MM-DDThh:mmTD')

would give me this:

2006-11-10T07:11CET

instead of this:

2006-11-10T07:11+02

Is there a way to get the timzone as an offset instead of as an abbreviation?


Using string manipulation, maybe? Like:

regexp_replace(current_date || 'T' || current_time, E':[^:]*?\\+', E'+');

or

replace( regexp_replace(now()::text, E':[^:]*?\\+', E'+'), ' ', 'T' );

Not elegant, but it gets the job done (tested with 8.3)


Maybe it's too late, but here is an alternative:

to_char(now(), 'YYYY-MM-DDThh:mi')::timestamp with time zone::text

Nota: I have replaced mm to mi in the above expression since I think that minutes are expected instead of months after the hour.

0

精彩评论

暂无评论...
验证码 换一张
取 消