I would like to convert datetime to UTC time. I try below code but the output looks like not correct:
import datetime import pandas as pd def str2dt(tstr): dt = datetime.datetime.strptime(tstr, '%m-%d %H:%M:%S.%f') return dt ts = "04-12 20:43:34.342" dt = str2dt(ts) utc_delta = datetime.datetime.utcnow() - datetime.datetime.now() utc = dt - utc_delta print(dt,'->',utc) Current output: 1900-04-12 20:43:34.342000 -> 1900-04-12 15:43:34.342001
The expected output time is 1900-04-12 02:43:34.342001
utc_delta?pytzfor timezone is what you need to convert between local vs UTC