python获取日期加减之后的日期
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python获取日期加减之后的日期相关的知识,希望对你有一定的参考价值。
python语言中的datetime模块可以利用其中的方法获取不同的日期,比如获取当前日期、明天、昨天、上个月、下个月和明年。下面利用几个实例说明这些日期的获取方法,操作如下:
第一步,利用datetime模块获取当前日期
datetime.date.today();
如下图所示:
第二步,获取当前日期前一天日期,利用当前日期减去一天,如下图所示:
第三步,获取当前日期后一天日期,利用当前日期加上一天,如下图所示:
第四步,获取当前日期下一个月日期,利用当前日期加上30天,如下图所示:
第五步,获取当前日期上一个月的日期,利用当前日期减去30天,如下图所示:
第六步,获取当前日期返回明年今天的日期,利用当前日期加上365天,如下图所示:
- python获取前后N天或前后N个月的日期
1 # -*- coding: utf-8 -*-
2
3 #-----------------------------------------------------------------------------------
4 import datetime
5 #获取366天前的日期
6 day=(datetime.date.today() - datetime.timedelta(days=366)).strftime(‘%Y-%m-%d‘)
7 print(day)
8 #获取366天后的日期
9 day=(datetime.date.today() + datetime.timedelta(days=366)).strftime(‘%Y-%m-%d‘)
10 print(day)
11 #-----------------------------------------------------------------------------------
12
13 ‘‘‘获取当前日期前后N天或N月的日期‘‘‘
14
15 from time import strftime, localtime
16 from datetime import timedelta, date
17 import calendar
18
19
20 year = strftime("%Y", localtime())
21 mon = strftime("%m", localtime())
22 day = strftime("%d", localtime())
23 hour = strftime("%H", localtime())
24 min = strftime("%M", localtime())
25 sec = strftime("%S", localtime())
26
27
28 def today():
29 ‘‘‘‘‘
30 get today,date format="YYYY-MM-DD"
31 ‘‘‘‘‘
32 return date.today()
33
34 def todaystr():
35 ‘‘‘
36 get date string, date format="YYYYMMDD"
37 ‘‘‘
38 return year + mon + day
39
40 def datetime():
41 ‘‘‘‘‘
42 get datetime,format="YYYY-MM-DD HH:MM:SS"
43 ‘‘‘
44 return strftime("%Y-%m-%d %H:%M:%S", localtime())
45
46 def datetimestr():
47 ‘‘‘‘‘
48 get datetime string
49 date format="YYYYMMDDHHMMSS"
50 ‘‘‘
51 return year + mon + day + hour + min + sec
52
53 def get_day_of_day(n=0):
54 ‘‘‘‘‘
55 if n>=0,date is larger than today
56 if n<0,date is less than today
57 date format = "YYYY-MM-DD"
58 ‘‘‘
59 if (n < 0):
60 n = abs(n)
61 return date.today() - timedelta(days=n)
62 else:
63 return date.today() + timedelta(days=n)
64
65 def get_days_of_month(year, mon):
66 ‘‘‘‘‘
67 get days of month
68 ‘‘‘
69 return calendar.monthrange(year, mon)[1]
70
71 def get_firstday_of_month(year, mon):
72 ‘‘‘‘‘
73 get the first day of month
74 date format = "YYYY-MM-DD"
75 ‘‘‘
76 days = "01"
77 if (int(mon) < 10):
78 mon = "0" + str(int(mon))
79 arr = (year, mon, days)
80 return "-".join("%s" % i for i in arr)
81
82 def get_lastday_of_month(year, mon):
83 ‘‘‘‘‘
84 get the last day of month
85 date format = "YYYY-MM-DD"
86 ‘‘‘
87 days = calendar.monthrange(year, mon)[1]
88 mon = addzero(mon)
89 arr = (year, mon, days)
90 return "-".join("%s" % i for i in arr)
91
92 def get_firstday_month(n=0):
93 ‘‘‘‘‘
94 get the first day of month from today
95 n is how many months
96 ‘‘‘
97 (y, m, d) = getyearandmonth(n)
98 d = "01"
99 arr = (y, m, d)
100 return "-".join("%s" % i for i in arr)
101
102 def get_lastday_month(n=0):
103 ‘‘‘‘‘
104 get the last day of month from today
105 n is how many months
106 ‘‘‘
107 return "-".join("%s" % i for i in getyearandmonth(n))
108
109 def getyearandmonth(n=0):
110 ‘‘‘‘‘
111 get the year,month,days from today
112 befor or after n months
113 ‘‘‘
114 thisyear = int(year)
115 thismon = int(mon)
116 totalmon = thismon + n
117 if (n >= 0):
118 if (totalmon <= 12):
119 days = str(get_days_of_month(thisyear, totalmon))
120 totalmon = addzero(totalmon)
121 return (year, totalmon, days)
122 else:
123 i = totalmon / 12
124 j = totalmon % 12
125 if (j == 0):
126 i -= 1
127 j = 12
128 thisyear += i
129 days = str(get_days_of_month(thisyear, j))
130 j = addzero(j)
131 return (str(thisyear), str(j), days)
132 else:
133 if ((totalmon > 0) and (totalmon < 12)):
134 days = str(get_days_of_month(thisyear, totalmon))
135 totalmon = addzero(totalmon)
136 return (year, totalmon, days)
137 else:
138 i = totalmon / 12
139 j = totalmon % 12
140 if (j == 0):
141 i -= 1
142 j = 12
143 thisyear += i
144 days = str(get_days_of_month(thisyear, j))
145 j = addzero(j)
146 return (str(thisyear), str(j), days)
147
148 def addzero(n):
149 ‘‘‘‘‘
150 add 0 before 0-9
151 return 01-09
152 ‘‘‘
153 nabs = abs(int(n))
154 if (nabs < 10):
155 return "0" + str(nabs)
156 else:
157 return nabs
158
159 def get_today_month(n=0):
160 ‘‘‘‘‘
161 获取当前日期前后N月的日期
162 if n>0, 获取当前日期前N月的日期
163 if n<0, 获取当前日期后N月的日期
164 date format = "YYYY-MM-DD"
165 ‘‘‘
166 (y, m, d) = getyearandmonth(n)
167 arr = (y, m, d)
168 if (int(day) < int(d)):
169 arr = (y, m, day)
170 return "-".join("%s" % i for i in arr)
171
172
173 if __name__ == "__main__":
174 print today()#获取当前日期,2017-12-02
175 print todaystr()#20171202
176 print datetime()#2017-12-02 16:37:19
177 print datetimestr()#20171202163719
178 print get_day_of_day(20)#获取20天后的日期,2017-12-22
179 print get_day_of_day(-3)#获取3天前的日期,2017-11-29
180 print get_today_month(-3)#获取3个月前的日期, 2017-09-02
181 print get_today_month(3)# 获取3个月后的日期, 2018-03-02
182 print get_today_month(19)# 获取19个月后的日期,2019-07-02
以上是关于python获取日期加减之后的日期的主要内容,如果未能解决你的问题,请参考以下文章