python3 时间和日期

一.时间模块time

1.获取时间戳 time.time(),可以利用时间戳差异值计算程序运行多少时间

1 import  time
2 timestamp=time.time()
3 print('当前的时间戳为:',timestamp)
4 ----------------------------------------------------
5 当前的时间戳为: 1522241598.2945554
time.time()

2.获取当前时间time.localtime(time.time())

1 import  time
2 localtime = time.localtime(time.time())
3 print ("本地时间为 :", localtime)
4 #(4位数年,月,日,小时,分钟,秒,一周的第几日,一年的第几日,夏令时)
5 #(year,mon,mday,hour,min,sec,wday,yday,isdst)
6 ---------------------------------------------------------------------------
7 本地时间为 : time.struct_time(tm_year=2018, tm_mon=3, tm_mday=28, tm_hour=21, tm_min=0, tm_sec=51, tm_wday=2, tm_yday=87, tm_isdst=0)
time.localtime(time.time())

3.获取格式化时间time.asctime(),可以画图

1 import  time
2 localtime = time.asctime(time.localtime(time.time()))
3 print ("本地时间为 :", localtime)
4 ---------------------------------------------------------
5 本地时间为 : Wed Mar 28 21:03:49 2018
time.asctime()

4.时间元组转化为格式化日期:time.strftime(fmt[,tupletime])接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定

1 import  time
2 print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
3 #%Y 四位数的年份表  %m 月份   %d 月内中的一天
4 #%H 24小时制小时数  %M 分钟数  %S 秒
5 ------------------------------------------------------------
6 2018-03-28 21:10:09
time.strftime()

5.格式化日期转化为字符串:strptime(日期字符串,格式符字符串)和mktime(时间元组)

6.获取当前cpu时间 .clock(),用来统计一段代码的执行耗时

1 import  time
2 start=time.clock()
3 for i in range(0,1000):
4     print(i)
5 end=time.clock()
6 print(end-start)
7 ------------------------------------------
8 0.004529955984085713
clock

7.程序休眠n秒 sleep()(暂停程序)

 1 import  time
 2 while True:
 3     result=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 4     print(result)
 5     time.sleep(1)#时间休眠一秒
 6 -------------------------------------------------------
 7 2018-04-15 20:56:38
 8 2018-04-15 20:56:39
 9 2018-04-15 20:56:40
10 2018-04-15 20:56:41
11 2018-04-15 20:56:42
12 
13 ....
time.sleep

二.日历模块Calendar

1.打印日历

 1 import calendar
 2 cal = calendar.month(2018, 4)
 3 print ("以下输出2018年4月份的日历:\n",cal)
 4 ---------------------------------------------------------
 5 以下输出2018年4月份的日历:
 6       April 2018
 7 Mo Tu We Th Fr Sa Su
 8                    1
 9  2  3  4  5  6  7  8
10  9 10 11 12 13 14 15
11 16 17 18 19 20 21 22
12 23 24 25 26 27 28 29
13 30
日历

 三.datatime模块(处理日期和时间的标准库)

该模块中除了datatime类,还有data类,time类

 1 import datetime
 2 
 3 #获取当天日期
 4 print(datetime.datetime.now())
 5 print(datetime.datetime.today())
 6 
 7 
 8 t=datetime.datetime.now()
 9 
10 #单独获取当前的年月日时分秒
11 print(t.year)
12 print(t.month)
13 print(t.day)
14 print(t.hour)
15 print(t.minute)
16 print(t.second)
17 
18 
19 #计算n天之后的日期
20 result=t+datetime.timedelta(days=7)
21 print(result)
22 
23 
24 #获取两个日期时间的时间差
25 first=datetime.datetime(2018,4,15,12,3,5)
26 second=datetime.datetime(2019,4,15,12,3,5)
27 delta=second-first
28 print(delta)
29 print(delta.total_seconds())#获取时间间隔的秒数
30 ------------------------------------------------------------------------------------
31 2018-04-15 21:10:15.969052
32 2018-04-15 21:10:15.969052
33 2018
34 4
35 15
36 21
37 10
38 15
39 2018-04-22 21:10:15.969052
40 365 days, 0:00:00
41 31536000.0
datatime

 

猜你喜欢

转载自www.cnblogs.com/yu-liang/p/8666370.html