How can I change the format of a date
var_time="2021-05-17T00:00:00Z"
to 2021-05-17 00:00:00
?
How can I change the format of a date
var_time="2021-05-17T00:00:00Z"
to 2021-05-17 00:00:00
?
The Data format you have is ISO8601 so you need to convert this to the desired format. Here is what I did:
from datetime import datetime
s= "2021-05-17T00:00:00Z"
yourdate = datetime.fromisoformat(s.replace('Z','+00:00'))
yourdate.strftime('%Y-%m-%d %H:%M:%S')
The Output will be this:
'2021-05-17 00:00:00'
I don't know if this is the best way but I hope I could help.
Source:
The below should work after taking off the trailing "Z" :
from datetime import datetime
new_var_time = datetime.fromisoformat(var_time[:-1]).strftime("%Y-%m-%d %H:%M:%S")
I am not sure whether your var_time is a string or a time/datetime obeject? If this variable is a string, you can use the following:
import datetime
string = '2021-05-17T00:00:00Z'
date_dt1 = datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')
print(date_dt1)
print:
2021-05-17 00:00:00