1

How to convert 2015 June 1 into date format in python like date_object = datetime.date(2014, 12, 4)

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Vineetha K D
  • 211
  • 1
  • 5
  • 13

2 Answers2

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
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