How to convert 2015 June 1 into date format in python like date_object = datetime.date(2014, 12, 4)
Asked
Active
Viewed 2,617 times
1

Anand S Kumar
- 88,551
- 18
- 188
- 176

Vineetha K D
- 211
- 1
- 5
- 13
2 Answers
3
You can use the format - '%Y %B %d'
along with datetime.datetime.strptime()
method to convert string to date. Where %Y
is 4 digit year, %B
is complete month name, and %d
is date.
Example/Demo -
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d')
datetime.datetime(2015, 6, 1, 0, 0)
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d').date()
datetime.date(2015, 6, 1)
Use the first one, if you are content with datetime
object, if you want the date()
object itself, you can use the second one.

Anand S Kumar
- 88,551
- 18
- 188
- 176
-
Thank u , how can i pass month as a variable and that convert in to corresponding digits? ie, month = self.request.post('month'); – Vineetha K D Aug 14 '15 at 06:54
-
after parsing the month , you can use `date.month` to get its digit. – Anand S Kumar Aug 14 '15 at 06:57
-
@VineethaKD I see that you have unaccepted the answer, did you face any issue? Was the answer wrong? – Anand S Kumar Aug 14 '15 at 07:04
-
No your answer is helpful for me, but have an issue when the month is pass as a variable – Vineetha K D Aug 14 '15 at 07:11
-
What do you mean as a variable? Can you show how you are passing it? – Anand S Kumar Aug 14 '15 at 07:14
-
@VineethaKD Also, I believe if the answer helped you , you should accept it and if you have a new question, ask a new question by clicking on the `Ask a question` button on top. – Anand S Kumar Aug 14 '15 at 07:17
-
Also, if you want I can continue to help you, just let me know what you meant by variable? how is it passed. – Anand S Kumar Aug 14 '15 at 07:26
0
You can use the date constructor
>>> from datetime import date
>>> date_object = date(year=2015, month=6, day=1)
>>> print date_object
2015-06-01

Palmer
- 493
- 4
- 13