I have this string (It's come from a variable. as a example, st_date)
'2015-01-28 03:00:00'
and I want to parse the date and convert to type:date
datetime.date 2015-01-28
from it.. How could I do that?
I have this string (It's come from a variable. as a example, st_date)
'2015-01-28 03:00:00'
and I want to parse the date and convert to type:date
datetime.date 2015-01-28
from it.. How could I do that?
Use datetime.strptime() which takes two arguments, your date as a string and the format you want.
from datetime import datetime
my_date = datetime.strptime('2015-01-28 03:00:00', '%Y-%m-%d %H:%M:%S')
my_date.year
>> 2015
import datetime
import time
date_str_obj = '2015-01-28 03:00:00'
date_date_obj = datetime.datetime.strptime(date_str_obj, '%Y-%m-%d %I:%M:%f')
Just read the docs https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime
from datetime import datetime
a = '2015-01-28 03:00:00'
print datetime.strptime(a[:10], '%Y-%m-%d')
Use the strptime() function.
datetime.datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %H:%M:%S') #24-hour clock
datetime.datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %I:%M:%S') #12-hour clock
You can use easy_date to make it easy:
import date_converter
my_date = date_converter.string_to_date('2015-01-28 03:00:00', '%Y-%m-%d %H:%M:%S')
from datetime import datetime
date = datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %H:%M:%S')
date.date()
Use dateutil:
import dateutil.parser
dateutil.parser.parse('2015-01-28 03:00:00').date()
>>datetime.date(2015, 1, 28)