Postgresql时间戳long转日期,转字符串,转timestamp,日期转换错误定位及解决

Posted 程序媛一枚~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Postgresql时间戳long转日期,转字符串,转timestamp,日期转换错误定位及解决相关的知识,希望对你有一定的参考价值。

1. 效果图如下

排查问题时遇到个尴尬的问题,程序里计算好的小时开始及结束的时间戳,转换发现有点乱。
遂研究下,终于正确的转换了。

记录下原因: postgresql的 timestamp可以由bigint秒时间戳10位通过 to_timestamp转换出来;

  • bigint 转 timestamp to_timestamp
  • string 转 bigint cast
  • timestamp 转 字符串 to_char
  • ‘YYYY-MM-DD HH24:MI:SS’ 年月日24小时制

2. sql

select 1659351600000,1659355199999,to_char(to_timestamp(1659351600000),'yyyy-mm-dd hh:mm:ss') as msts,
to_char(to_timestamp(1659355199999),'yyyy-mm-dd hh:mm:ss') as mste,
to_char(to_timestamp(1659351600),'yyyy-mm-dd hh:mm:ss') as sts,
to_char(to_timestamp(1659355199),'yyyy-mm-dd hh:mm:ss') as stE,
to_char(to_timestamp(1659351600),'YYYY-MM-DD HH24:MI:SS') as ts24,
to_char(to_timestamp(1659355199),'YYYY-MM-DD HH24:MI:SS') as te24;
-- 字符串转bigint,毫秒转秒,时间戳转timestamp,timestamp转字符串,timestamp转date,char转date
select 1659351600000,1659355199999,
cast('1659351600000' as bigint) as char2bigint,
cast('1659351600000' as bigint)/1000 as s,
to_timestamp(1659355199)::DATE as date,
to_date('2022-08-01 19:59:59.000000', 'yyyy-mm-dd hh24:mi:ss.us' ) as date2,
to_char(to_timestamp(cast('1659351600000' as bigint)/1000),'YYYY-MM-DD HH24:MI:SS') as char2ts24,
to_char(to_timestamp(1659351600000),'yyyy-mm-dd hh:mm:ss') as msts,
to_char(to_timestamp(1659355199999),'yyyy-mm-dd hh:mm:ss') as mste,
to_char(to_timestamp(1659351600),'yyyy-mm-dd hh:mm:ss') as sts,
to_char(to_timestamp(1659355199),'yyyy-mm-dd hh:mm:ss') as stE,
to_char(to_timestamp(1659351600),'YYYY-MM-DD HH24:MI:SS') as ts24,
to_char(to_timestamp(1659355199),'YYYY-MM-DD HH24:MI:SS') as te24,
to_char(to_timestamp(1659355199),'yyyy-mm-dd hh24:mi:ss.us') as te24_2

以上是关于Postgresql时间戳long转日期,转字符串,转timestamp,日期转换错误定位及解决的主要内容,如果未能解决你的问题,请参考以下文章

js中怎么吧long型的日期转换成String类型的

Postgresql 常用命令

js页面里时间戳转日期

sql 时间戳转日期字符串

MySQL日期 字符串 时间戳互转

mysql时间戳转换日期格式