JulianGindi
JulianGindi

Reputation: 75

Refactor Python Code

I was wondering if anyone could help me refactor the following Python code:

In this example, endDate is a string like such: "2012-08-22"

dateArray = [int(x) for x in endDate.split('-')]
event.add('dtend', datetime(dateArray[0], dateArray[1], dateArray[2]))

I appreciate it!

Upvotes: 3

Views: 171

Answers (2)

Prashant Kumar
Prashant Kumar

Reputation: 22539

from datetime import strptime
event.add('dtend', strptime(endDate, '%Y-%m-%d')

Upvotes: 6

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

Better use datetime.strptime:

>>> from datetime import datetime
>>> strs = "2012-08-22"
>>> datetime.strptime(strs,'%Y-%m-%d')
datetime.datetime(2012, 8, 22, 0, 0)

For your code:

event.add('dtend', datetime.strptime(endDate,'%Y-%m-%d'))

Upvotes: 4

Related Questions