Home > Net >  Converting a specific time stamp of hours:minutes:seconds from a log and turning it into seconds onl
Converting a specific time stamp of hours:minutes:seconds from a log and turning it into seconds onl

Time:05-09

So I'm grabbing the values a2 and a3 from a csv file, and in order to write out my equation I need the value a4 in seconds not hours:minutes:seconds which currently shows as 0:01:01 (61 seconds) when I print a4.

a2 = tow3.loc[38, 'TimeString']
a2 = datetime.strptime(a2, '%d-%b-%Y %H:%M:%S')
print(a2.strftime('%M:%S'))


a3 = tow3.loc[98, 'TimeString']
a3 = datetime.strptime(a3, '%d-%b-%Y %H:%M:%S')
print(a3.strftime('%M:%S'))


a4 = a3-a2
print(a4)

My current execution

CodePudding user response:

Use:

d1 = pd.date_range('05-12-2021', '06-12-2021', 10)
d2 = pd.date_range('06-12-2021', '07-12-2021', 10)
(d2-d1).seconds

Output:

Int64Index([0, 76800, 67200, 57600, 48000, 38400, 28800, 19200, 9600, 0], dtype='int64')

CodePudding user response:

If (a3-a2) is a datetime.timedelta type, you can then use

(a3-a2).total_seconds()

To have the whole timedelta difference converted in seconds

  • Related