2个格式错误的日期时间之间的小时和分钟 [英] Hours and minutes between 2 incorrectly formatted datetimes

查看:99
本文介绍了2个格式错误的日期时间之间的小时和分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在数据库中有一些时间戳,我想从中获得时和分的差异

So i have some timestamps in a DB and i want to get the hours and minutes difference from them

问题是时间戳部分的格式不正确,其中小时始终为12,分钟部分实际上是小时,秒部分实际上是分钟。

The problem is the timestamp portion is formatted incorrectly where the hour is always 12 and the minutes portion is actually the hours and the seconds is actually the minutes.

示例数据库时间戳记:2020年10月1日12:08:52 AM

Example DB timestamp: 10/1/2020 12:08:52 AM

因此在上面的示例中,时间实际上是8:52 AM而不是12:08 AM

So in the above example the time is actually 8:52 AM not 12:08 AM

我如何将此日期时间转换为我可以用来计算分钟差的东西和这两个奇怪格式的时间戳之间的小时数?

How can i convert this datetime to something i can use in order to calculate the difference in minutes and hours between these 2 oddly formatted timestamps?

我理想的最终目标是显示HH:MM格式的差异

My ideal end goal is something that displays the difference in the HH:MM format

编辑: oracle中的时间戳实际上如下所示,在这个eaxmple中12表示没有任何意义,而18实际是小时。

the timestamps in oracle actually look like below, and in this eaxmple the 12 means nothing and 18 is actually the hours.

我正在寻找的示例:

01-OCT-20 12.18.44.000000000 AM-01-OCT-20 12.12.42.000000000 AM

01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM

输出:06:02。因此在这种情况下,时间跨度为6小时2分钟。

Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.

谢谢

推荐答案

是这样吗?

(我的默认日期格式是Oracle中的'yyyy-mm-dd hh24:mi:ss'。 ..)

(my default date format is 'yyyy-mm-dd hh24:mi:ss' in Oracle ...)

WITH 
indata(sdb) AS (
            SELECT '10/1/2020 12:08:52 AM' FROM dual
  UNION ALL SELECT '10/1/2020 12:08:52 PM' FROM dual
)
SELECT 
  TO_TIMESTAMP(sdb,'dd/mm/yyyy 12:hh:mi AM') AS ts
FROM indata;
-- out          ts          
-- out ---------------------
-- out  2020-01-10 08:52:00
-- out  2020-01-10 20:52:00

这篇关于2个格式错误的日期时间之间的小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆