strftime()
expects a date object, so the string will need to be converted to a date object before it can be altered by strftime()
.
Use datetime.strptime()
to convert a string to a date object: it uses the same syntax as strftime to identify the datetime elements of the input string. In the case of "17 Jan 2020", that process would look like this:
from datetime import datetime
datetime_string = "17 Jan 2020"
datetime_object = datetime.strptime(datetime_string, '%d %b %Y')
print(datetime_object.date()) # a datetime object
formatted_string_output = datetime.strftime(datetime_object, '%Y-%m-%dT%H:%M:%S.%fZ')
Note that %f
will output 6 digits (microseconds, not milliseconds): use something like the solution suggested here to tailor your output to match your needs.