Converting String to Date Format in Python -
i have following date need convert:
wed, 09 jul 2014 12:22:17 +0000
this stored string. wrote code convert date format want (the string above passed argument covertdate function):
def convertdate(dictvalue): date_string = dictvalue format_string = '%a, %d %b %y %h:%m:%s %z' date_object = datetime.datetime.strptime(date_string, format_string) date_correct_form = date_object.strftime("%y-%m-%d") print(type(date_correct_form)) print(date_correct_form) return date_correct_form
the output follows:
<class 'str'> 2014-10-30
i format want, still isn't recognized date.
how can make so?
you returning date_correct_form
, result of strftime
:
return string representing date, controlled explicit format string.
(emphasis mine)
if want datetime object, return date_object
. if need both, can return both:
return date_correct_form, date_object
you can call so:
date_string, date_obj = convertdate(dictvalue)
you have formatted string in date_string
, , if still need logic against datetime object, in date_obj
Comments
Post a Comment