9

how can i convert my string of date to a datetime.timedelta() in Python?

I have this code :

import datetime

date_select = '2011-12-1'
delta = datetime.timedelta(days=1)
target_date = date_select + delta
print target_date

thanks in advance ...

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
gadss
  • 21,687
  • 41
  • 104
  • 154
  • What's wrong with the code you posted? Your question doesn't match the example code at all. The question asks about making a `timedelta`, but the code creates a 'datetime`. What are you asking? – S.Lott Dec 03 '11 at 04:02

3 Answers3

11

You wouldn't convert date_select to a timedelta, instead, you need a datetime object, which can be added to a timedelta to produce an updated datetime object:

from datetime import datetime, timedelta

date_select = datetime.strptime('2011-12-1', '%Y-%m-%d')
delta = timedelta(days=1)
target_date = date_select + delta
print target_date

Or, if you prefer, without the fancy from ... import ... import line:

import datetime    # <- LOOK HERE, same as in your example

date_select = datetime.datetime.strptime('2011-12-1', '%Y-%m-%d')
delta = datetime.timedelta(days=1)
target_date = date_select + delta
print target_date
Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
  • thanks for the reply Adam, but it also not working.. i got this error : `AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'` do you have any ideas? – gadss Dec 03 '11 at 04:09
  • 1
    @gadss, Sorry, that's because I've imported timedelta from datetime. You can either use your original import line, and change the strptime to `datetime.datetime.strptime(...)` or change your timedelta line to `timedelta(...)` (notice, no `datetime.`). The example I've given should work though. – Adam Wagner Dec 03 '11 at 04:13
  • when i print the `target_date` this will come out `2011-12-01 00:00:00` is there any way that only the `2011-12-01` will be return to `target_date`? thanks ... – gadss Dec 03 '11 at 05:28
0
from datetime import datetime, timedelta

date_select = '2011-12-1'
new_data = datetime.strptime(date_select, '%Y-%m-%d')
delta = timedelta(days=1)

target_date = date_select + delta
print target_date

You will get 2011-12-02 00:00:00; to strip off the '00:00:00' and get only the date, just add .date() to target_date

print target_date.date()

This should give you the only the date = 2011-12-02

Faruq
  • 1
  • 1
0

You use strptime to do this.

from datetime import datetime

target_date = datetime.strptime(date_select, '%Y-%m-%d')
Blender
  • 289,723
  • 53
  • 439
  • 496
  • `AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'` this is what i have got.. thanks blender – gadss Dec 03 '11 at 04:12
  • I can run this code just fine on Python2 and Python3. Do you have any more code? – Blender Dec 03 '11 at 04:15
  • @gadss, that's because `datetime` in his example (notice the different import line) is the object/type and not the module `datetime`. This can be a bit confusing if you aren't used to using this module, but the object/type and the module have exactly the same name. – Adam Wagner Dec 03 '11 at 04:16
  • @AdamWagner, with "from datatime import datetime", still got this error. With this log, strptime cannot translate timedelta to string. – liuyang1 Jul 13 '15 at 11:58