简体   繁体   中英

Extract date,month,year and month name from the unix timestamp with postgresql

I use postgres for the rails app and I have a unix timestamp in postgresql db. I have a requirement to select and group by the dd-mm-yyyy and by month name .

Consider I have the following unix timestamp

1425148200

and I would need to change this to datetime and I used to_timestamp which returned

2015-02-28 18:30:00 UTC

and I tried to convert the datetime to local timezone using

::timestamp without time zone AT TIME ZONE 'IST'

but that did not give time in required timezone and instead it returned

2015-02-28 16:30:00 UTC

and I tried to get the date part using ::date which returned

Sat, 28 Feb 2015

So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp.

Thanks in Advance!

select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month')

01-03-2015 & of course March

It is postgres mistake I guess according to http://www.postgresql.org/docs/7.2/static/timezones.html 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM