本文实例为大家分享了python实现简单日期工具类的具体代码,供大家参考,具体内容如下
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
|
import datetime import time datetime_format = "%y-%m-%d %h:%m:%s" time_format = "%h:%m:%s" #当前毫秒数 def curmilis(): return int (time.time() * 1000 ) #当前秒数 def curseconds(): return int (time.time()) #当前日期 格式%y-%m-%d %h:%m:%s def curdatetime(): return datetime.datetime.strftime(datetime.datetime.now(),datetime_format) #当前日期 格式%y-%m-%d def curdate(): return datetime.date.today() #当前时间 格式%y-%m-%d def curtime(): return time.strftime(time_format) #秒转日期 def secondstodatetime(seconds): return time.strftime(datetime_format,time.localtime(seconds)) #毫秒转日期 def milistodatetime(milix): return time.strftime(datetime_format,time.localtime(milix / / 1000 )) #日期转毫秒 def datetimetomilis(datetimestr): strf = time.strptime(datetimestr,datetime_format) return int (time.mktime(strf)) * 1000 #日期转秒 def datetimetoseconds(datetimestr): strf = time.strptime(datetimestr,datetime_format) return int (time.mktime(strf)) #当前年 def curyear(): return datetime.datetime.now().year #当前月 def curmonth(): return datetime.datetime.now().month #当前日 def curday(): return datetime.datetime.now().day #当前时 def curhour(): return datetime.datetime.now().hour #当前分 def curminute(): return datetime.datetime.now().minute #当前秒 def cursecond(): return datetime.datetime.now().second #星期几 def curweek(): return datetime.datetime.now().weekday() #几天前的时间 def nowdaysago(days): daysagotime = datetime.datetime.now() - datetime.timedelta(days = days) return time.strftime(datetime_format,daysagotime.timetuple()) #几天后的时间 def nowdaysafter(days): daysagotime = datetime.datetime.now() + datetime.timedelta(days = days) return time.strftime(datetime_format,daysagotime.timetuple()) #某个日期几天前的时间 def dtimedaysago(dtimestr,days): daysagotime = datetime.datetime.strptime(dtimestr,datetime_format) - datetime.timedelta(days = days) return time.strftime(datetime_format,daysagotime.timetuple()) #某个日期几天前的时间 def dtimedaysafter(dtimestr,days): daysagotime = datetime.datetime.strptime(dtimestr,datetime_format) + datetime.timedelta(days = days) return time.strftime(datetime_format,daysagotime.timetuple()) secondstamp = curseconds() print ( "当前秒:" ,secondstamp) milisstamp = curmilis() print ( "当前毫秒:" ,milisstamp) curdtime = curdatetime() print ( "当前时间:" ,curdtime) curdate = curdate() print ( "当前日期:" ,curdate) curt = curtime() print ( "当前时刻:" ,curt) stdtime = secondstodatetime(secondstamp) print ( "秒转时间:" ,stdtime) mtdtime = milistodatetime(milisstamp) print ( "毫秒转时间:" ,mtdtime) dtimetm = datetimetomilis(mtdtime) print ( "时间转毫秒:" ,dtimetm) dtimets = datetimetoseconds(mtdtime) print ( "时间转秒:" ,dtimets) year = curyear() print ( "年:" ,year) month = curmonth() print ( "月:" ,month) day = curday() print ( "日:" ,day) hour = curhour() print ( "时:" ,hour) minute = curminute() print ( "分:" ,minute) second = cursecond() print ( "秒:" ,second) week = curweek() print ( "星期:" ,week) |
输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
当前秒: 1518341913 当前毫秒: 1518341913403 当前时间: 2018 - 02 - 11 17 : 38 : 33 当前日期: 2018 - 02 - 11 当前时刻: 17 : 38 : 33 秒转时间: 2018 - 02 - 11 17 : 38 : 33 毫秒转时间: 2018 - 02 - 11 17 : 38 : 33 时间转毫秒: 1518341913000 时间转秒: 1518341913 年: 2018 月: 2 日: 11 时: 17 分: 38 秒: 33 星期: 6 [finished in 0.2s ] |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/maosijunzi/article/details/79312324