62

I have this string:

'2012-02-10' # (year-month-day)

and I need it to be as date type for me to use the date function isoweekday().

Does anyone know how I can convert this string into a date?

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
Lucas Rezende
  • 2,516
  • 8
  • 25
  • 34
  • 1
    This would help for someone who also needs all date and time: > datetime.datetime.strptime('2020-10-13 00:00:00', '%Y-%m-%d %H:%M:%S') – Han Van Pham Dec 14 '20 at 08:40

5 Answers5

127

While it seems the question was answered per the OP's request, none of the answers give a good way to get a datetime.date object instead of a datetime.datetime. So for those searching and finding this thread:

datetime.date has no .strptime method; use the one on datetime.datetime instead and then call .date() on it to receive the datetime.date object.

Like so:

>>> from datetime import datetime
>>> datetime.strptime('2014-12-04', '%Y-%m-%d').date()
datetime.date(2014, 12, 4)
Darian Moody
  • 3,565
  • 1
  • 22
  • 33
  • 9
    This is the real answer – Nyxynyx Nov 05 '17 at 15:48
  • This should be the accepted answer – liamsuma Oct 27 '20 at 15:35
  • If I convert the datetime.datetime object to a datetime.date object, the datetime.date object doesn't work with the datetime.timedelta() method. Is there a way to do date calculations (such as add or subtract days from a date) if the object is datetime.date? – SQA777 Mar 16 '21 at 21:13
68

You can do that with datetime.strptime()

Example:

>>> from datetime import datetime
>>> datetime.strptime('2012-02-10' , '%Y-%m-%d')
datetime.datetime(2012, 2, 10, 0, 0)
>>> _.isoweekday()
5

You can find the table with all the strptime directive here.


To increment by 2 days if .isweekday() == 6, you can use timedelta():

>>> import datetime
>>> date = datetime.datetime.strptime('2012-02-11' , '%Y-%m-%d')
>>> if date.isoweekday() == 6:
...     date += datetime.timedelta(days=2)
... 
>>> date
datetime.datetime(2012, 2, 13, 0, 0)
>>> date.strftime('%Y-%m-%d')   # if you want a string again
'2012-02-13'
Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
  • That was exactly what I did: if datetime.strptime(a, "%Y-%m-%d").isoweekday() == 6: PONTO_MOVIMENTO = a – Lucas Rezende Feb 29 '12 at 18:26
  • Just one more question... how can I add days to my date? For example, if isoweekdday() == 6, i need to increment the date in 2 days. Have idea how I do that? Thanks again! – Lucas Rezende Feb 29 '12 at 18:27
  • I found out already using timedelta. But thanks! – Lucas Rezende Feb 29 '12 at 18:36
  • @user1072627: Yes `timdelta` is the right tool. Even though you've already found it I added an example anyway. :) – Rik Poggi Feb 29 '12 at 18:49
  • This converts a string to a datetime. The OP asked how to convert a string to a date. If I print your converted datetime I get `2012-02-13 00:00:00` which is not a date and does not match the format specified in `strptime`. – Jonathan Rys Dec 08 '20 at 16:30
5
from datetime import datetime

a = datetime.strptime(f, "%Y-%m-%d")
bgporter
  • 35,114
  • 8
  • 59
  • 65
f p
  • 3,165
  • 1
  • 27
  • 35
3

Use datetime.datetime.strptime:

>>> import datetime
>>> date = datetime.datetime.strptime('2012-02-10', '%Y-%m-%d')
>>> date.isoweekday()
5
phihag
  • 278,196
  • 72
  • 453
  • 469
3
>>> from datetime import datetime
>>> year, month, day = map(int, my_date.split('-'))
>>> date_object = datetime(year, month, day)
Fred
  • 1,011
  • 1
  • 10
  • 36
  • 1
    I used something like this when I needed a datetime.date object, NOT a datetime.datetime object. Unfortunately, datetime.date does not have a strptime() method. – CraigO Sep 14 '13 at 20:42