Python date addition and subtraction, python date addition and subtraction
Some date operations:
Some operations on Date: 1 # convert date to string and get the date after the specified (or system date) n days -- @ Eillot 2 def dataTimeToString (dsNow = ReservationTime (). get ("workerDate"), dsDays = 1): 3 "4: param dsNow: indicates the date and time of today, in seconds 5: param dsDays: represents the date after n days (hours, minutes, And seconds remain unchanged), the default value is 6 "7 # convert workerDate string to date string => datetime 8 workerDateTo_Datetime = datetime. datetime. strptime (dsNow, '% Y-% m-% D') 9 delta = datetime. timedelta (dsDays) 10 ndays_after = workerDateTo_Datetime + delta11 return ndays_after.strftime ('% Y-% m-% D') 12 13 # It is add (addition) and sub (subtraction ), which for "starttime" and "endtime ". @ eillot14 def timeOperation (Operationstarttime, Operationendtime, flag = 0): 15 16 "17: param Operationstarttime: 18: param Operationendtime: 19: param flag: used to mark the addition and subtraction of time, the default value is 0, indicating 20 "21 order_starttime = datetime after 1 hour of time. datetime. strptime (Operationstarttime, '% H: % m') 22 order_endtime = datetime. datetime. strptime (Operationendtime, '% H: % m') 23 delta = datetime. timedelta (hours = 1) 24 # datetime type: 14:00:00 converted to str, and formatted and output 25 add_order_starttime = (order_starttime + delta) According to '% H: % M ). strftime ('% H: % m') 26 add_order_endtime = (order_endtime + delta ). strftime ('% H: % m') 27 if flag = # Return to starttime and endtime29 add_order_starttime = (order_starttime-delta) one hour ago ). strftime ('% H: % m') 30 add_order_endtime = (order_endtime-delta ). strftime ('% H: % m') 31 return add_order_starttime, add_order_endtime # The returned value type is tuple (tuples)