简体   繁体   English

使用PostgreSQL从Unix时间戳中提取日期,月份,年份和月份名称

[英]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. 我为rails应用程序使用postgres,并且在postgresql db中有一个unix timestamp I have a requirement to select and group by the dd-mm-yyyy and by month name . 我需要根据dd-mm-yyyymonth name进行选择和group by

Consider I have the following unix timestamp 考虑我有以下unix时间戳

1425148200 1425148200

and I would need to change this to datetime and I used to_timestamp which returned 我需要将其更改为datetime,并使用to_timestamp返回

2015-02-28 18:30:00 UTC 2015-02-28 18:30:00 UTC

and I tried to convert the datetime to local timezone using 我试图使用将datetime时间转换为local timezone

::timestamp without time zone AT TIME ZONE 'IST' ::不带时区的时间戳记时区“ IST”

but that did not give time in required timezone and instead it returned 但这没有在所需的时区给定时间,而是返回了

2015-02-28 16:30:00 UTC 2015-02-28 16:30:00 UTC

and I tried to get the date part using ::date which returned 我试图使用::date获取日期部分,该日期部分返回

Sat, 28 Feb 2015 2015年2月28日,星期六

So please help me get the dd-mm-yyyy in specified timezone and month name(March) from the unix timestamp. 因此,请帮助我从Unix时间戳获取指定timezone和月份名称(3月)中的dd-mm-yyyy

Thanks in Advance! 提前致谢!

select to_char(to_timestamp('1425148200')::timestamptz at time zone 'UTC-5:30','DD-MM-YYYY & of course Month') 选择to_char(to_timestamp('1425148200'):: timestamptz在时区'UTC-5:30','DD-MM-YYYY,当然还有月份')

01-03-2015 & of course March 2015年1月3日,当然还有三月

It is postgres mistake I guess according to http://www.postgresql.org/docs/7.2/static/timezones.html 根据http://www.postgresql.org/docs/7.2/static/timezones.html我猜这是postgres错误 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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