转换上午下午 时间
2021/5/26 上午 03:09:00
def transfer_date(self, date_str): import datetime if '上午' in date_str: date_list = date_str.split('上午') format_time = datetime.datetime.strptime(date_list[0].strip(), '%Y/%m/%d') hour_list = date_list[1].strip().split(':', 1) if int(hour_list[0]) + 12 > 23: real_hour = int(hour_list[0]) - 12 real_times = datetime.datetime.strftime(format_time, '%Y-%m-%d') hour_str = '0' + str(real_hour) if len(str(real_hour)) == 1 else str(real_hour) date_strs = real_times + ' ' + hour_str + ':' + hour_list[1] else: real_times = datetime.datetime.strftime(format_time, '%Y-%m-%d') date_strs = real_times + ' ' + date_list[1].strip() elif '下午' in date_str: date_list = date_str.split('下午') format_time = datetime.datetime.strptime(date_list[0].strip(), '%Y/%m/%d') hour_list = date_list[1].strip().split(':', 1) if int(hour_list[0]) + 12 > 23: real_times = datetime.datetime.strftime(format_time, '%Y-%m-%d') date_strs = real_times + ' ' + date_list[1].strip() else: real_hour = int(hour_list[0]) + 12 real_times = datetime.datetime.strftime(format_time, '%Y-%m-%d') hour_str = '0' + str(real_hour) if len(str(real_hour)) == 1 else str(real_hour) date_strs = real_times + ' ' + hour_str + ':' + hour_list[1] else: date_strs = '' return date_strs