Detailed explanation of date addition and subtraction operations in python

黄舟
Release: 2017-08-13 10:01:48
Original
3778 people have browsed it

Some operations on dates:


对日期的一些操作:

 1 #日期转化为字符串并得到指定(或系统日期)n天后的日期--@Eillot
 2 def dataTimeToString(dsNow=ReservationTime().get("workerDate"), dsDays=1):
 3     """
 4     :param dsNow: 表示今天的日期跟时分秒
 5     :param dsDays:表示n天后的日期(时分秒不变),默认值为1天后
 6     """
 7     #将workerDate字符串转换为日期 string => datetime
 8     workerDateTo_Datetime=datetime.datetime.strptime(dsNow,'%Y-%m-%d')
 9     delta=datetime.timedelta(dsDays)
10     ndays_after=workerDateTo_Datetime+delta
11     return ndays_after.strftime('%Y-%m-%d')
12 
13 #It is add(加法) and sub(减法) , which for "starttime" and "endtime".@eillot
14 def timeOperation( Operationstarttime, Operationendtime,flag=0):
15 
16     """
17     :param Operationstarttime: 
18     :param Operationendtime:
19     :param flag: 用于标记time的加减法,默认值我为0,表示time的1小时之后
20     """
21     order_starttime=datetime.datetime.strptime(Operationstarttime,'%H:%M')
22     order_endtime = datetime.datetime.strptime(Operationendtime,'%H:%M')
23     delta=datetime.timedelta(hours=1)
24     #datetime type:1900-01-01 14:00:00转化为str,并按'%H:%M'格式化输出
25     add_order_starttime =  (order_starttime + delta).strftime('%H:%M')
26     add_order_endtime = (order_endtime + delta).strftime('%H:%M')
27     if flag == 1:
28          #回到1小时前的starttime跟endtime
29          add_order_starttime =  (order_starttime - delta).strftime('%H:%M')
30          add_order_endtime = (order_endtime - delta).strftime('%H:%M')
31     return add_order_starttime , add_order_endtime#返回值类型为tuple(元组)
Copy after login

The above is the detailed content of Detailed explanation of date addition and subtraction operations in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template