0

I have two timestamps

starttime = '2020-10-13T13:42:01Z'
endtime = '2020-10-13T13:42:21Z'

When I convert it to datetime, the seconds of the timestamp changes

timestampstart = pd.to_datetime(starttime, format="%Y-%m-%dT%H:%M:%S%fZ")
timestampend = pd.to_datetime(endtime, format="%Y-%m-%dT%H:%M:%S%fZ")

print(timestampstart,timestampend)
2020-10-13 13:42:00.100000 2020-10-13 13:42:02.100000

My required output is

2020-10-13 13:42:01 2020-10-13 13:42:21

What is the problem? How can I tackle it?

phoenix
  • 328
  • 2
  • 12

1 Answers1

1

As commented, your timestamps are formatted according to ISO8601. The T separates date and time, the Z stands for "zulu time", i.e. UTC. The correct strptime format code would be

'%Y-%m-%dT%H:%M:%S%z'

Note that there are no fractional seconds (%f) and instead of a literal Z, you should use %z to convert the Z to UTC. You could also use Python datetime.fromisoformat for parsing with a little tweak, see e.g. here.


But: since the format is standardized and you seem to be working with pandas, you can automatically parse as e.g.

import pandas as pd
pd.to_datetime('2020-10-13T13:42:01Z')

Out[3]: Timestamp('2020-10-13 13:42:01+0000', tz='UTC')
FObersteiner
  • 22,500
  • 8
  • 42
  • 72