Python-模块之时间模块(time,datetime)

1、标准库datetime(实验测试,在最后)

  datetime模块:

    对日期、时间、时间戳的处理

    datetime类:

      * 类方法:

        • today() 返回本地时区的datetime对象
        • now(tz= None)返回当前时间的datetime对象,时间到微秒,如果tz 为None,返回和today() 一样
        • utcnow() 没有时区的当前时间
        • fromtimestamp(timestamp,tz= None)从一个时间戳 返回一个datetime 对像

      * datetime对象:

        • timestamp() 返回一个到微秒的时间戳
          • 时间戳:格林威治时间1970\1\1 0点到现在的描述
        • 构造方法  datetime.datetime(2016,12,6,29,24,79023)
        • year、month、day、hour、minute、second、microsecond,取datetime对象的年月日时fenmiao及微秒
        • weekday() 返回星期的天,周一0,周日6
        • isoweekday() 返回星期的天,周一1 周期7
        • date() 返回日期date对象
        • time() 返回时间time对象
        • replace() 修改并返回新的时间
        • isocalendar() 返回一个三元组(年,周数,周的天)
 1 import datetime
 2 
 3 d1 = datetime.datetime.today()
 4 print(d1) # 2018-08-16 19:26:50.206136
 5 
 6 d2 = datetime.datetime.now() # 跟时区有关的时间
 7 print(d2)
 8 
 9 d3 = datetime.datetime.utcnow() # 没有时区的当前时间
10 print(d3) # 2018-08-16 11:32:17.610938 和 d1 差8小时,8个时区
11 
12 d4 = datetime.datetime.fromtimestamp(1534419463.917267)
13 print(d4) # 2018-08-16 19:37:43.917267
14 
15 d5 = datetime.datetime.now().timestamp() # datetime对象的时间戳
16 print(d5) # 1534419463.917267 #1970到现在
17 
18 格林威治时间与时区没关系,不同时区的时间通过+-时区得出,或者根据uctnow()得出无时区时间,通过+-时区来算当前时区时间。
19 如:在不同时区的同时执行命令获取datetime对象,在算时间戳是一样的。
20 
21 
22 d6 = datetime.datetime(2016,12,6,14,29,24,79023)
23 print(d6) # 2016-12-06 14:29:24.079023
24 
25 
26 d7 = datetime.datetime.now().year
27 print(d7)
28  
29 d8 = datetime.datetime.now().weekday()
30 print(d8)
31 
32 d9 = datetime.datetime.now().isoweekday()
33 print(d9)
34 
35 d10 = datetime.datetime.now().date()
36 print(d10) # 2018-08-16
37 
38 d11 = datetime.datetime.now().time()
39 print(d11) # 20:07:27.327642
40 
41 d12 = datetime.datetime.now().isocalendar()
42 print(d12)  # (2018, 33, 4) # 三元组
test

 

   日期格式化:

      • 类方法 strptime(date_string, format) , 返回datetime 对象
      • 对象方法 strftime(format),返回字符串
      • 字符串format 函数格式化  
1 import datetime
2 
3 d = datetime.datetime.strptime('21/11/04 16:30', '%d/%m/%y %H:%M')
4 print(d) # 2004-11-21 16:30:00
5 
6 print(d.strftime('%Y_%m_%d %H_%S')) # 2004_11_21 16_00
7 
8 print('{0:%Y}--{0:%m}--{0:%d}'.format(d)) # 2004--11--21
test

 

    timedelta 对象

      • datetime2 =datetime1 + timedelta
      • datetime2 = datetime1 - timedelta
      • timedelta = datetime 1-datetime2
      • 构造方法:
        • datetime.timedelta(days= 0,seconds=0,microseconds = 0 , milliseconds=0,minutes=0,hours=0,weeks=0)
        • year = datetime.timedelta(days=365)
      • total_secondes() 返回时间差的总秒数。
1 mport datetime
2 d = datetime.timedelta(days=345,seconds=10)
3 print(d)
4 # 差345天10秒

 

2、标准库time:

  time.sleep(secs) 将调用县城挂起指定的秒数。

 

 1 import  datetime
 2 print('---------------类方法---------------------')
 3 t = datetime.datetime.today()
 4 print(t) # 2018-09-12 17:01:45.074973
 5 
 6 t = datetime.datetime.now()
 7 print(t) # 2018-09-12 17:02:15.087199
 8 
 9 # 加时区的 now()
10 t = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=0)))
11 print(t) # 2018-09-12 09:11:25.969086+00:00
12 t = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))
13 print(t) # 2018-09-12 17:09:46.697203+08:00
14 
15 t = datetime.datetime.utcnow()
16 print(t) # 2018-09-12 09:12:00.155404
17 
18 t = datetime.datetime.fromtimestamp(1534419463.917267)
19 print(t) # 2018-08-16 19:37:43.917267
20 
21 print('---------------对象---------------------')
22 
23 t = datetime.datetime.now()# datetime 对象
24 print(t.timestamp(), type(t.timestamp())) # 1536743811.451235 # <class 'float'>
25 print('', t.year, type(t.year)) # <class 'int'>
26 print('', t.month)
27 print(t.weekday()) # 返回本周的第几天,周一为0
28 print(t.isoweekday())# 周一为 1
29 print(t.date()) # 2018-09-12
30 print(t.time()) # 17:24:38.023756
31 print(t.isocalendar()) # (2018, 37, 3)
32 
33 
34 # 构造一个时间对象
35 print(datetime.datetime(2019,1,12,23,12,15)) # 2019-01-12 23:12:15
36 
37 print('---------------日期格式化---------------------')
38 # 类方法:
39 t = datetime.datetime.strptime('2017/01/01 00:00:01 +0800', '%Y/%m/%d %H:%M:%S %z')
40 print(t) # 2017-01-01 00:00:01+08:00
41 t = datetime.datetime.strptime('07/Apr/2017:09:32:46 +0800', '%d/%b/%Y:%H:%M:%S %z') # %b 月的缩写,%B 月的全写
42 print(t) # 2017-04-07 09:32:46+08:00
43 
44 # 对象方法:
45 t = datetime.datetime.now()
46 print(t) # 2018-09-12 17:31:21.986405
47 timestr = t.strftime('%Y-%m-%d %H-%M-%S')
48 print(timestr) # 2018-09-12 17-33-09
49 print(type(timestr)) # <class 'str'>
50 
51 print('---------------timedelta对象---------------------')
52 t = datetime.timedelta(seconds=5)
53 print(t) # 0:00:05, 不写单位 默认day + 时间
test

 

posted @ 2018-08-16 20:27  JerryZao  阅读(326)  评论(0编辑  收藏  举报