I have a datetime string in the following format: 2021-02-25T04:39:55Z.
Can I parse it to datetime object without explicitly telling Python how?
strptime() requires you to set the format.
I have a datetime string in the following format: 2021-02-25T04:39:55Z.
Can I parse it to datetime object without explicitly telling Python how?
strptime() requires you to set the format.
pandas can also be used
import pandas as pd
datetime_str = "2021-02-25T04:39:55Z"
my_datetime = pd.to_datetime(datetime_str).to_pydatetime()
print(my_datetime)
print(type(my_datetime))
# output
# 2021-02-25 04:39:55+00:00
# <class 'datetime.datetime'>
You can create a new function and call it strptime that calls datetime.strptime:
from datetime import datetime
def strptime(time_, time_fmt=None):
time_fmt = time_fmt or r"Your Own Default Format Here"
return datetime.strptime(time_, time_fmt)
Other than that I prefer if you utilize dateutil as recommended above.