- working on list of datetime to string
most of the examples useddatetime.strptime('Jun 1 2005', '%b %d %Y').date()
Convert string "Jun 1 2005 1:33PM" into datetime
which can only put one one input at a time, but I am reciving the entire string such as
customer_date_list = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-01-14', '2010-12-13', '2010-01-12', '2010-2-11', '2010-02-07', '2010-12-02', '2011-11-30']
my expect output is
['2010-01-12', '2010-01-14', '2010-02-07', '2010-02-11', '2010-12-02', '2010-12-13', '2011-02-04', '2011-06-02', '2011-08-05', '2011-11-30']
the code below: I'm making either of those code work:
list1_date_string = [datetime.strftime(fs, "%Y, %m, %d, %H, %M") for fs in list1_date]
dateStr = list1_date.strftime("%Y, %m, %d, %H, %M")
- the overall code
import datetime
def date_sorting_operation(input_list):
list1_date = [datetime.datetime.strptime(ts, "%Y-%m-%d") for ts in input_list]
for i in range(len(list1_date)):
for i in range(len(list1_date) - 1):
if list1_date[i] > list1_date[i + 1]:
temporary = list1_date[i + 1]
list1_date[i + 1] = list1_date[i]
list1_date[i] = temporary
#list1_date_string = [datetime.strftime(fs, "%Y, %m, %d, %H, %M") for fs in list1_date]
#dateStr = list1_date.strftime("%Y, %m, %d, %H, %M")
return list1_date, type(list1_date)
customer_date_list = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-01-14', '2010-12-13', '2010-01-12', '2010-2-11', '2010-02-07', '2010-12-02', '2011-11-30']
print (date_sorting_operation(customer_date_list))