Hi,
I'm trying to take a date, add a day to it, and then convert it to a string. The date's format starts out like: '10/1/2009', and should end like: '2009-10-1'.
I can do that part, but then I can't convert it to a date format and add a day. Here's what I have so far:

#!/usr/bin/env python

import time
import datetime

		epdate = episode_content[entry_number][index2+13:index3].replace('/', '-') #reformat so that the slashes are replaced
		epdate = epdate.split('-') #split the string into three
		epdate = epdate[2] + '-' + epdate[0] + '-' + epdate[1] #reorder the date to the correct format
		epdate = epdate.replace(' ', '')#make sure there are no whitespaces
		episode_date.append (epdate) #add it to my dictionary
		epdate = time.strptime(epdate, '%Y-%m-%d') #convert epdate to time type
		epdatestart = datetime.datetime(*time.strptime(epdate, '%Y-%m-%d')[:6]) #save as start date in datetype form
		epdateend = datetime.datetime(*time.strptime(epdate, '%Y-%m-%d')[:6]) + datetime.timedelta(days=1) #save as end date in datetype form
		#epdate2 = epdate + datetime.timedelta(days=1)
		print str(epdatestart)
		print str(epdateend)

At the moment, the error I get is caused by the 'strptime':

TypeError: expected string of buffer

Any help is appreciated
Thanks

Standard way is to have seconds since some starting time or day with floating point number (say Visual Basic).

#!/usr/bin/env python

episode_date=[]
epdatestart = '10/1/2009'
epdate = epdatestart.split('/')
epdateend = epdate[2]+'-' + epdate[0]+'-' + epdate[1]

print str(epdatestart)
print str(epdateend)

""" Output:
>>> 
10/1/2009
2009-10-1
>>> """

Edited 6 Years Ago by pyTony: n/a

So revized code after little datetime study:

#!/usr/bin/env python
from datetime import datetime as dt
from datetime import timedelta as td

epdatestart = '11/30/2009' ## mm/dd/yyyy
print epdatestart
epdate = epdatestart.split('/')

start=dt(int(epdate[2]),int(epdate[0]),int(epdate[1]))
print start.date()
tomorrow=start+td(days=1)
print tomorrow.date()

Edited 6 Years Ago by pyTony: n/a

Nicer, in my opinion is this (which is basically the same as tonyjv's code). I have added the possibility to parse dates with various separators, and made the code a little more self-documenting. I have not protected against alternate input date formats.

import datetime
import re

datesep = re.compile('[/.-]') # accept '/', '-' or '.' as date separators
oneday = datetime.timedelta(days=1)

datestring = '01/23/2010' # get this however you need to

mm,dd,yy = [int(x) for x in datesep.split(datestring)]
dateobj = datetime.date(yy,mm,dd)
print dateobj+oneday # you can format the date output if needed

Nicer, in my opinion is this (which is basically the same as tonyjv's code). I have added the possibility to parse dates with various separators,

You can do this if you want to use any, but only one separator in date.
If you want to limit separators you can replace the not s.isdigit() part of code. Separator printing is just for demonstration of the list comprehension.

## request: possibility to parse dates with various separators
import datetime

timedelta=datetime.timedelta

for datestring in ['01/23/2010','01.12.2020','1.1.1976',
                   '2 2 2000', ## if you do not make extra limitations this goes also
                   '3d2m1998' ## this gets the message
                   ]:
    sep=[s for s in datestring if not s.isdigit()]

    if len(sep)!=2 or sep[0] != sep [1]: print "Mixup separators",sep
    else:
        print sep ## for testing
        datesep=sep[0]
        mm,dd,yy = (int(part) for part in datestring.split(datesep))
        print datetime.date(yy,mm,dd)+ timedelta(days=1)
    
"""Output:
>>> 
['/', '/']
2010-01-24
['.', '.']
2020-01-13
['.', '.']
1976-01-02
[' ', ' ']
2000-02-03
Mixup separators ['d', 'm']
"""

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.