forked from liuyuzhou/python3.7sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_time_all_style.py
More file actions
178 lines (159 loc) · 4.5 KB
/
Copy pathdate_time_all_style.py
File metadata and controls
178 lines (159 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/python
#-*- coding:UTF-8 -*-
import calendar
import datetime
import time
year = time.strftime("%Y", time.localtime())
mon = time.strftime("%m", time.localtime())
day = time.strftime("%d", time.localtime())
hour = time.strftime("%H", time.localtime())
min = time.strftime("%M", time.localtime())
sec = time.strftime("%S", time.localtime())
def today():
'''''
get today,date format="YYYY-MM-DD"
'''''
return datetime.date.today()
def todaystr():
'''
get date string, date format="YYYYMMDD"
'''
return year + mon + day
def date_time():
'''''
get datetime,format="YYYY-MM-DD HH:MM:SS"
'''
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def datetimestr():
'''''
get datetime string
date format="YYYYMMDDHHMMSS"
'''
return year + mon + day + hour + min + sec
def get_day_of_day(n=0):
'''''
if n>=0,date is larger than today
if n<0,date is less than today
date format = "YYYY-MM-DD"
'''
if n < 0:
n = abs(n)
return datetime.date.today() - datetime.timedelta(days=n)
else:
return datetime.date.today() + datetime.timedelta(days=n)
def get_days_of_month(year, mon):
'''''
get days of month
'''
return calendar.monthrange(year, mon)[1]
def get_firstday_of_month(year, mon):
'''''
get the first day of month
date format = "YYYY-MM-DD"
'''
days = "01"
if int(mon) < 10:
mon = "0" + str(int(mon))
arr = (year, mon, days)
return "-".join("%s" %i for i in arr)
def get_lastday_of_month(year, mon):
'''''
get the last day of month
date format = "YYYY-MM-DD"
'''
days=calendar.monthrange(year, mon)[1]
mon = addzero(mon)
arr = (year, mon, days)
return "-".join("%s" %i for i in arr)
def get_firstday_month(n=0):
'''''
get the first day of month from today
n is how many months
'''
(y, m, d) = getyearandmonth(n)
d = "01"
arr = (y, m, d)
return "-".join("%s" %i for i in arr)
def get_lastday_month(n=0):
'''''
get the last day of month from today
n is how many months
'''
return "-".join("%s" %i for i in getyearandmonth(n))
def getyearandmonth(n=0):
'''''
get the year,month,days from today
befor or after n months
'''
thisyear = int(year)
thismon = int(mon)
totalmon = thismon + n
if n >= 0:
if totalmon <= 12:
days = str(get_days_of_month(thisyear, totalmon))
totalmon = addzero(totalmon)
return year, totalmon, days
else:
i = totalmon//12
j = totalmon%12
if j == 0:
i -= 1
j = 12
thisyear += i
days = str(get_days_of_month(thisyear, j))
j = addzero(j)
return str(thisyear), str(j), days
else:
if totalmon > 0 and totalmon < 12:
days = str(get_days_of_month(thisyear,totalmon))
totalmon = addzero(totalmon)
return year, totalmon, days
else:
i = totalmon//12
j = totalmon%12
if(j==0):
i -= 1
j = 12
thisyear += i
days = str(get_days_of_month(thisyear, j))
j = addzero(j)
return str(thisyear), str(j), days
def addzero(n):
'''''
add 0 before 0-9
return 01-09
'''
nabs = abs(int(n))
if nabs < 10:
return "0" + str(nabs)
else:
return nabs
def get_today_month(n=0):
'''''
获取当前日期前后N月的日期
if n>0, 获取当前日期前N月的日期
if n<0, 获取当前日期后N月的日期
date format = "YYYY-MM-DD"
'''
(y, m, d) = getyearandmonth(n)
arr = (y, m, d)
if int(day) < int(d):
arr = (y, m, day)
return "-".join("%s" %i for i in arr)
def get_firstday_month(n=0):
(y, m, d) = getyearandmonth(n)
arr = (y, m, '01')
return "-".join("%s" %i for i in arr)
def main():
print(f'today is:{today()}')
print(f'today is:{todaystr()}')
print(f'the date time is:{date_time()}')
print(f'data time is:{datetimestr()}')
print(f'2 days after today is:{get_day_of_day(2)}')
print(f'2 days before today is:{get_day_of_day(-2)}')
print(f'2 months after today is:{get_today_month(2)}')
print(f'2 months before today is:{get_today_month(-2)}')
print(f'2 months after this month is:{get_firstday_month(2)}')
print(f'2 months before this month is:{get_firstday_month(-2)}')
if __name__=="__main__":
main()