I have a string timestamp. I want to convert it to seconds and then again later I want to convert that seconds into a timestamp. So for this, I converted the string to timestamp first then calculated seconds. Again I converted that seconds into a timestamp.
But converted timestamp is not matching with the original one. How can I fix this problem?
I used the below code for this conversion:
import datetime
import pandas as pd
def string_to_datetime(string):
dv = [string]
dv_series = pd.Series(dv)
dv_to_datetime = pd.to_datetime(dv_series)
dv_to_datetime_list = list(dv_to_datetime)
return dv_to_datetime_list[0]
timestamp = ['2020-06-12T08:33:14']
timestamp = string_to_datetime(timestamp[0])
print('timestamp:')
print(timestamp)
timestamp_in_seconds = float((timestamp - datetime.datetime(1970, 1, 1)).total_seconds())
print('timestamp in seconds:')
print(timestamp_in_seconds)
print('Again converted to timestamp:')
print(datetime.datetime.fromtimestamp(timestamp_in_seconds))
The output of the above code is like below:
timestamp:
2020-06-12 08:33:14
timestamp in seconds:
1591950794.0
Again converted to timestamp:
2020-06-12 14:33:14
I got two solutions. Maybe it will be helpful for someone later.
Timezone information is important during converting back to the original timestamp.