【问题标题】:Convert date string to timestamp with timezone in python在python中将日期字符串转换为带有时区的时间戳
【发布时间】:2021-03-09 04:55:14
【问题描述】:

我有一个日期字符串 2020-10-14, 14:13:23,我想在 python 中将其转换为 timestamp with timezone 格式,以便我可以在我的 postgresql 表中删除具有此特定时间戳的行

请建议如何进行?

【问题讨论】:

    标签: python-3.x postgresql timestamp timestamp-with-timezone


    【解决方案1】:

    你想cast吗?

    select '2020-10-14, 14:13:23'::timestamp with time zone
    
    |时间戳 | | :--------------------- | | 2020-10-14 14:13:23+01 |

    如果你想过滤与这个字面值属于同一秒的时间戳:

    select *
    from mytable
    where execution_date >= '2020-10-14, 14:13:23'::timestamp with time zone
      and execution_date <  '2020-10-14, 14:13:23'::timestamp with time zone + interval '1 second'
    

    【讨论】:

    • 我做了select '2020-10-14, 14:13:23'::timestamp with time zone from table_name,我得到了 16 行,但实际上该表只包含带有此时间戳的两行。我做错了吗?
    • @Mahesh:请提供样本数据和期望的结果。更好的是,您能否设置一个 dbfiddle.uk 来演示该问题?
    • 嘿,我无法在小提琴中提供样本数据从未使用过它。但我想要做的是获取具有此时间戳的所有行。所以我写了查询select * from table_name where execution_date in (select '2020-10-14, 14:13:23'::timestamp with time zone),但它返回0行。 execution_date 是上图中的列名
    • @Mahesh:您的数据有小数秒,而不是您传递的值。所以没有完全匹配。
    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2016-09-15
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多