英文:
How to calculate time difference of text like YYYYMMDDHHMMSSXXX format including millisecond
问题
20230101232324548 - 20230101232324500 = 48
在Linux中我尝试了expr a-b
,但对于一些记录结果不正确。有没有办法在Linux或Excel中计算时间差。任何可能的方法都会很有帮助。
英文:
Want to calculate time difference in milliseconds for below format YYYYMMDDHHMMSSXXX.
XXX refers milliseconds.
20230101232324548 - 20230101232324500 =48
Im trying expr a-b in linux but its not giving proper results for few records.
Any way we can calculate difference either in linux or excel. Any possible way would be helpful.
答案1
得分: 2
这不够优雅,但在Excel中,您可以按以下方式处理日期:
=DATE(LEFT(A2,4),MID(A2,5,2),MID(A2,7,2))+MID(A2,9,2)/24+MID(A2,11,2)/1440+MID(A2,13,2)/86400+RIGHT(A2,3)/86400000
或者按毫秒处理:
=DATE(LEFT(A2,4),MID(A2,5,2),MID(A2,7,2))*86400000+MID(A2,9,2)*3600000+MID(A2,11,2)*60000+MID(A2,13,2)*1000+RIGHT(A2,3)
然后相减结果。
按毫秒处理似乎是更成功的选项。
(即使使用第二种方法,您也开始接近Excel的精度限制,所以如果您希望将此应用到遥远的未来,您可能希望在执行其余的计算之前,减去两个日期(假设它们相对接近)。)
英文:
This is not elegant but in Excel you could work in days like this:
=DATE(LEFT(A2,4),MID(A2,5,2),MID(A2,7,2))+MID(A2,9,2)/24+MID(A2,11,2)/1440+MID(A2,13,2)/86400+RIGHT(A2,3)/86400000
or in milliseconds like this:
=DATE(LEFT(A2,4),MID(A2,5,2),MID(A2,7,2))*86400000+MID(A2,9,2)*3600000+MID(A2,11,2)*60000+MID(A2,13,2)*1000+RIGHT(A2,3)
then subtract the results.
Working in milliseconds appears the more successful option.
(even with the second method you are starting to approach the limit on Excel's precision, so if you wanted to apply this a long way into the future you might want to subtract the two dates (assuming they are fairly close to each other) before carrying out the rest of the calculation).
答案2
得分: 1
=LET(a, A1:A3,
b, B1:B3,
dif, LEFT(a, LEN(a)-3)
- LEFT(b, LEN(b)-3),
ms, RIGHT(a, 3)
- RIGHT(b, 3),
IF(dif=0, TEXT(ms, "@"), dif & ms))
英文:
If stored as text and the result returned will be text, you could use:
=LET(a, A1:A3,
b, B1:B3,
dif,LEFT(a,LEN(a)-3)
-LEFT(b,LEN(b)-3),
ms, RIGHT(a,3)
-RIGHT(b,3),
IF(dif=0, TEXT(ms,"@"), dif&ms))
It divides the string into portions excel can handle and subtract these. DateTime excluding milliseconds at first and milliseconds afterwards. If the DateTime difference equals 0 it's omitted.
答案3
得分: 1
抱歉之前回答不够好。
bash脚本: diffdate.sh
#! /usr/bin/env bash
D1="$1"
D2="$2"
# 将日期从"YYYYMMDDHHMMSSNNN"转换为"YYYY-MM-DDTHH:MM:SS"(ISO8601)
D1D="${D1:0:4}-${D1:4:2}-${D1:6:2}T${D1:8:2}:${D1:10:2}:${D1:12:2}"
# 将日期从"YYYYMMDDHHMMSSNNN"转换为"NNN"
D1N="${D1:14:3}"
# 将ISO日期转换为从1970-01-01以来的毫秒 + "NNN"
D1S=$(date -d "${D1D}" +"%s")${D1N}
# 将日期从"YYYYMMDDHHMMSSNNN"转换为"YYYY-MM-DDTHH:MM:SS"(ISO8601)
D2D="${D2:0:4}-${D2:4:2}-${D2:6:2}T${D2:8:2}:${D2:10:2}:${D2:12:2}"
# 将日期从"YYYYMMDDHHMMSSNNN"转换为"NNN"
D2N="${D2:14:3}"
# 将ISO日期转换为从1970-01-01以来的毫秒 + "NNN"
D2S=$(date -d "${D2D}" +"%s")${D2N}
# 差值
DIFFS=$((D1S - D2S))
echo "${DIFFS}"
通过以下命令使其可执行:
> chmod +x diffdate.sh
这样执行:
> ./diffdate.sh 20230101232324548 20230101232324500
48
> ./diffdate.sh 20230101232324648 20230101232324500
148
> ./diffdate.sh 20230101232324548 20230101232324600
-52
结果以毫秒表示。你想要将结果转换为分钟、小时、天吗?
英文:
Sorry for previous really bad answer.
The bash script: diffdate.sh
#! /usr/bin/env bash
D1="$1"
D2="$2"
# Date conversion from "YYYYMMDDHHMMSSNNN" to "YYYY-MM-DDTHH:MM:SS" (ISO8601)
D1D="${D1:0:4}-${D1:4:2}-${D1:6:2}T${D1:8:2}:${D1:10:2}:${D1:12:2}"
# Date conversion from "YYYYMMDDHHMMSSNNN" to "NNN"
D1N="${D1:14:3}"
# ISO date conversion to milliseconds from 1970-01-01 + "NNN"
D1S=$(date -d "${D1D}" +"%s")${D1N}
# Date conversion from "YYYYMMDDHHMMSSNNN" to "YYYY-MM-DDTHH:MM:SS" (ISO8601)
D2D="${D2:0:4}-${D2:4:2}-${D2:6:2}T${D2:8:2}:${D2:10:2}:${D2:12:2}"
# Date conversion from "YYYYMMDDHHMMSSNNN" to "NNN"
D2N="${D2:14:3}"
# ISO date conversion to milliseconds from 1970-01-01 + "NNN"
D2S=$(date -d "${D2D}" +"%s")${D2N}
# Diff
DIFFS=$(( D1S - D2S ))
echo "${DIFFS}"
Executable with the command:
> chmod +x diffdate.sh
Executed like this:
> ./diffdate.sh 20230101232324548 20230101232324500
48
> ./diffdate.sh 20230101232324648 20230101232324500
148
> ./diffdate.sh 20230101232324548 20230101232324600
-52
Result is in milliseconds.
Do you want the result with minutes, hours, days ?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论