Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor Python Code

Tags:

python

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!

like image 857
JulianGindi Avatar asked Jan 25 '26 04:01

JulianGindi


2 Answers

from datetime import strptime
event.add('dtend', strptime(endDate, '%Y-%m-%d')
like image 188
Prashant Kumar Avatar answered Jan 26 '26 19:01

Prashant Kumar


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'))
like image 45
Ashwini Chaudhary Avatar answered Jan 26 '26 18:01

Ashwini Chaudhary