1 defGetDateTime (TIME_STR):2 " "3 Conversion Time4 :p Aram Time_str:5 : return:6 " "7 if notisinstance (time_str,unicode):8Time_str = Time_str.decode ('Utf-8')9 TenTime_now =Datetime.datetime.now () OneTime_return =Time_str A - ifU'seconds' inchTime_str: -Reg = R'(\d+)' theT_second =Re.search (Reg, TIME_STR) - ifT_second is notNone: -T_second = T_second.group (1) -T_second =String.atoi (T_second) +T_second = Datetime.timedelta (seconds=T_second) -Time_return = Time_now-T_second +Time_return = Datetime.datetime.strftime (Time_return,'%y-%m-%d%h:%m:%s') A returnTime_return at elifU'minutes' inchTime_str: -Reg = R'(\d+)' -T_minute =Re.search (Reg, TIME_STR) - ifT_minute is notNone: -T_minute = T_minute.group (1) -T_minute =String.atoi (T_minute) inT_minute = Datetime.timedelta (minutes=T_minute) -Time_return = Time_now-T_minute toTime_return = Datetime.datetime.strftime (Time_return,'%y-%m-%d%h:%m:%s') + returnTime_return - elifU'hours' inchTime_str: theReg = R'(\d+)' *T_hour =Re.search (Reg, TIME_STR) $ ifT_hour is notNone:Panax NotoginsengT_hour = T_hour.group (1) -T_hour =String.atoi (T_hour) theT_hour = Datetime.timedelta (hours=t_hour) +Time_return = Time_now-T_hour ATime_return = Datetime.datetime.strftime (Time_return,'%y-%m-%d%h:%m:%s') the returnTime_return + elifU'Today' inchTime_str: -Time_return = Time_now.strftime ('%y-%m-%d%h:%m:%s') $ returnTime_return $ elifU'days ago' inchTime_str: -Reg = R'(\d+)' -T_day =Re.search (Reg, TIME_STR) the ifT_day is notNone: -T_day = T_day.group (1)WuyiT_day =String.atoi (t_day) theT_day = Datetime.timedelta (days=t_day) -Time_return = Time_now-T_day WuTime_return = Datetime.datetime.strftime (Time_return,'%y-%m-%d%h:%m:%s') - returnTime_return About elifU'Week' inchTime_str: $reg = U'(\d+)' -T_week =Re.search (Reg, TIME_STR) - ifT_week is notNone: -T_week = T_week.group (1) AT_week =String.atoi (T_week) +T_week = Datetime.timedelta (weeks=T_week) theTime_return = Time_now-T_week -Time_return = Datetime.datetime.strftime (Time_return,'%y-%m-%d%h:%m:%s') $ returnTime_return the elifU'Month' inchTime_str: theReg = R'(\d+)' theT_num =Re.search (Reg, TIME_STR) the ifT_num is notNone: -t_num = Int (T_num.group (1)) indate_to = Time_now.month-T_num the #judge whether it is a cross-year the ifDate_to = =0: AboutMonth_to = 12 theyear_to =-1 the Else: themonth_to = date_to% 12 +Year_to = (date_to)/12 - #if it is the last day of 31st, note that the other months do not have 31st theNow_year = time_now.year+year_toBayiD =Calendar.monthrange (now_year,month_to) theNow_day =Time_now.day the ifNow_day > D[1]: -Now_day = d[1] -Date_from =datetime.datetime (Now_year,month_to,now_day,time_now.hour,time_now.minute,time_now.second) theTime_return = Datetime.datetime.strftime (Date_from,'%y-%m-%d%h:%m:%s') the returnTime_return the elifU'years' inchTime_str: thereg = U'(\d+)' -T_year =Re.search (Reg, TIME_STR) the ifT_year is notNone: thet_year = Int (T_year.group (1)) theDate_from = Datetime.datetime (time_now.year-T_year,time_now.month,time_now.day,time_now.hour,time_now.minute,time_now.second)94Time_return = Datetime.datetime.strftime (Date_from,'%y-%m-%d%h:%m:%s') the returnTime_return the Else: the #Time_return = Time_str98Int_year =Time_now.year AboutStr_year =Str (int_year) - ifTime_str.find (Str_year) <0:101Time_return = Str_year +'-'+Time_str102T_count = Time_str.count (':')103 ifT_count = = 1:104Time_return + =': xx' the elifT_count = =0:106Time_return + ='00:00:00'107 108 #if the date is greater than the current date, the year is reduced by 1109R_date = Datetime.datetime.strptime (Time_return,'%y-%m-%d%h:%m:%s') the ifR_date >Time_now:111int_year_1 = Int_year-1 theTime_return =time_return.replace (str_year, str (int_year_1))113 the returnTime_return the the if __name__=='__main__':117 PrintGetDateTime ('1 weeks ago')View Code
Python Time Conversion