Stack Overflow Asked by coderWorld on December 16, 2021
I have 2 timestamps derived from OOZIE in this format:
2019-07-20T16:34:45.000Z
and say
2020-08-20T16:20:15.000Z
How can I find the time difference in seconds / mins / days? Which python library can help? Any suggestions?
I don't know about any library, But you can do it yourself. I am sharing a code that might work.
time_stamp1 = input("Time stamp1")
time_stamp2 = input("Time stamp2")
yrs = int(time_stamp1[:4]) - int(time_stamp2[:4])
mnths = int(time_stamp1[5:7]) - int(time_stamp2[5:7])
days = int(time_stamp1[8:10]) - int(time_stamp1[8:10])
hrs = int(time_stamp1[11:13]) - int(time_stamp2[11:13])
min = int(time_stamp1[14:16]) - int(time_stamp1[14:16])
sec = int(time_stamp1[17:19]) - int(time_stamp1[17:19])
ms = int(time_stamp1[20:23]) - int(time_stamp1[20:23])
print(yrs, mnths, days)
print(hrs, min, sec, ms)
You can use the abs() function if you don't want to know whether timestamp2 is older than timestamp 1 or not or vice versa.
Answered by Manan on December 16, 2021
You could use the datetime module.
First, use strptime to convert each of those timestamps into dates. The general format of it is:
from datetime import datetime
date1 = datetime.strptime("2019-07-20T16:34:45.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
date2 = datetime.strptime("2020-08-20T16:20:15.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
(Note: the second argument could be inaccurate, depending on the specifics of OOZIE). For info on how to construct a format/use strptime, see this and this for an introduction to strptime.
Then, you could just do t_diff = date1-date2
. This will return a timedelta object, with attributes for years, months, weeks, days, etc... microseconds). It also has a builtin .total_seconds()
method.
Hope this helped!
Answered by a1426 on December 16, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP