Convert string to date for actions

Hey guys,

I’m building a action and I have to convert a string to date format

'2022-04-05T09:07:06.798165Z'

And then I need to compare it to today date to see if seven days has passed or not.

Does anyone know what function I can use to do that using Python3?

Having today date:

current_date = datetime.datetime.now()

Getting date from array:

y = 0
daily_occurence_time = result.get("results")[y]["create_time"]

Now I need to convert this daily_occurence_time string to date and compare it with datetime.datetime.now()

Hello,

This is a purely Python problem, more suitable to be asked on Stack Overflow.

Anyway, here is how to do it with the format you have given:

from datetime import datetime

datetime_string = '2022-04-05T09:07:06.798165Z'
datetime_object = datetime.strptime(datetime_string, '%Y-%m-%dT%H:%M:%S.%fZ')

print(datetime_object, type(datetime_object))
# 2022-04-05 09:07:06.798165 <class 'datetime.datetime'>

difference = datetime.now() - datetime_object
print(difference, type(difference))
# 31 days, 11:58:33.307946 <class 'datetime.timedelta'>

Read this for the strptime reference and date formats: Python strptime() - string to datetime object

1 Like