# Convert string to date for actions

**URL:** <https://forum.rasa.com/t/convert-string-to-date-for-actions/53042>\
**Category:** Rasa Open Source\
**Created:** [May 6, 2022, 4:24pm UTC](https://forum.rasa.com/t/convert-string-to-date-for-actions/53042 "2022-05-06T16:24:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peixeprata](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/peixeprata/32/20551_2.png) [@peixeprata](https://forum.rasa.com/u/peixeprata)\
**Post date:** [May 6, 2022, 4:24pm UTC](https://forum.rasa.com/t/convert-string-to-date-for-actions/53042/1 "2022-05-06T16:24:34Z")

</div>

Hey guys,

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

```auto
'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:

```auto
current_date = datetime.datetime.now()

```

Getting date from array:

```auto
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()

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [May 6, 2022, 6:04pm UTC](https://forum.rasa.com/t/convert-string-to-date-for-actions/53042/2 "2022-05-06T18:04:40Z")

</div>

Hello,

This is a purely Python problem, more suitable to be asked on [Stack Overflow](https://stackoverflow.com/).

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

```python
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](https://www.programiz.com/python-programming/datetime/strptime)
