简体   繁体   English

将时间和日期转换为时间戳

[英]Convert time and date to timestamp

I have the following date:我有以下日期:

Wed Sep 16 2020 14:23:49 GMT+0000 (Coordinated Universal Time)

How I can convert it to timestamp?我如何将其转换为时间戳?

case:案件:

'Wed Sep 16 2020 14:23:49 GMT+0000 (Coordinated Universal Time)'::timestamp

doesn't works.不起作用。

Assuming you are working with the entire string as given:假设您正在使用给定的整个字符串:


select  to_timestamp('Wed Sep 16 2020 14:23:49 GMT+0000 (Coordinated Universal Time)', 'Dy Mon DD YYYY HH24:MI:SS GMT TZH')::timestamp;

to_timestamp     
---------------------
 2020-09-16 07:23:49


I'm in PDT so the time got rotated to that.我在PDT所以时间轮换了。

For more information on to_timestamp and the meaning of the elements of the format string, see here:有关to_timestamp和格式字符串元素含义的更多信息,请参见此处:

https://www.postgresql.org/docs/current/functions-formatting.html https://www.postgresql.org/docs/current/functions-formatting.html

Just cast() !只需cast()

select cast('Wed Sep 16 2020 14:23:49 GMT+0000' as timestamp)
select 'Wed Sep 16 2020 14:23:49 GMT+0000'::timestamp

Or, if you prefer to generate a timestamp with time zone :或者,如果您更喜欢生成timestamp with time zonetimestamp with time zone

select cast('Wed Sep 16 2020 14:23:49 GMT+0000' as timestamp with time zone)
select 'Wed Sep 16 2020 14:23:49 GMT+0000'::timestamp with time zone

Demo on DB Fiddle DB Fiddle 上的演示

A simple conversion works:一个简单的转换工作:

select 'Wed Sep 16 2020 14:23:49 GMT+0000'::timestamp 

Or if you want the timezone:或者,如果您想要时区:

select 'Wed Sep 16 2020 14:23:49 GMT+0000'::timestamptz

You can just take the initial portion:您可以只取初始部分:

select left('Wed Sep 16 2020 14:23:49 GMT+0000', 33)::timestamp 

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

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