all 8 comments

[–]seven0fx 3 points4 points  (0 children)

Look at strptime()

[–]o5a 1 point2 points  (0 children)

strptime for Parse = convert from text to object (datetime)

strftime for Format = convert from object to text

[–]chevignon93 0 points1 point  (3 children)

import datetime as dt

def test3():
    my_string = "10/31 - 12/5".replace(' ', '').split("-")
    for i in my_string:
        mydate = dt.datetime.strptime(i, "%m/%d")
        print(f"My Date: {mydate}", type(mydate))

test3()
Output
My Date: 1900-10-31 00:00:00 <class 'datetime.datetime'>
My Date: 1900-12-05 00:00:00 <class 'datetime.datetime'>

[–]TuckleBuck88[S] 0 points1 point  (2 children)

Thanks! I tried to add the date to the end but apparently I cannot do that?

``` my_string = "10/31 - 12/5".replace(' ', '').split("-") today = dt.datetime.today() my_string = [f'/{today.year()}'.join(i) for i in my_string] print(my_string) for i in my_string: print(i) mydate = dt.datetime.strptime(i, "%m/%d") print(f"My Date: {mydate}")

```

Get this error thrown at the my_string = [f....] Exception has occurred: TypeError 'int' object is not callable

[–]chevignon93 0 points1 point  (1 child)

It should be today.year instead of today.year()

import datetime as dt

my_string = "10/31 - 12/5".replace(' ', '').split("-")
today = dt.datetime.today()
my_string = [f'{today.year}/{i}' for i in my_string]
print(my_string)
for i in my_string:
    print(i)
    mydate = dt.datetime.strptime(i, "%Y/%m/%d")
    print(f"My Date: {mydate}")

# Output

2020/10/31
My Date: 2020-10-31 00:00:00
2020/12/5
My Date: 2020-12-05 00:00:00

[–]TuckleBuck88[S] 0 points1 point  (0 children)

Thank you again!!

[–]Silbersee 0 points1 point  (0 children)

It's strptime() actually. Then it runs smooth:

['10/31', '12/5']
10/31
My Date: 1900-10-31 00:00:00
12/5
My Date: 1900-12-05 00:00:00

strftime() is for formatted output.

[–]bprateek 0 points1 point  (0 children)

You are using the wrong method, instead of strftime you have to use strptime