1.介绍
在Python中处理时间相关的标准库,常用的有三个:time、datetime、calendar,它们分别负责不同的功能;
time:  提供时间和日期访问和转换的 函数。 
datetime:   用于处理日期和时间计算相关的类。 
calendar: 提供日历相关的   函数。 
2. time使用
2.1 当前时间
import time
  if __name__ == '__main__':     currentTime = time.time()     print("当前时间戳: {} 单位秒".format(currentTime))     print("当前时间戳: {} 单位秒".format(int(currentTime)))     print("当前时间戳: {} 单位毫秒".format(int(currentTime * 1000)))          time_tuple = time.localtime(time.time())     print("当前时间信息:", time_tuple)     
  当前时间戳: 1692195782.747853 单位秒 当前时间戳: 1692195782 单位秒 当前时间戳: 1692195782747 单位毫秒 当前时间信息: time.struct_time(tm_year=2023, tm_mon=8, tm_mday=16, tm_hour=22, tm_min=23, tm_sec=2, tm_wday=2, tm_yday=228, tm_isdst=0)
 
  | 
 
| 字段 | 
值说明 | 
tm_year  | 
年: 2008 | 
tm_mon   | 
月: 1 到 12 | 
tm_mday  | 
天:  1 到 31 | 
tm_hour  | 
小时:  0 到 23 | 
tm_min   | 
分钟:  0 到 59 | 
tm_sec   | 
秒:  0 到 61 (60或61 是闰秒) | 
tm_wday  | 
周几:   0到6 (0是周一) | 
tm_yday  | 
一年中第几天(1 到 366) | 
tm_isdst | 
是否夏令时;1:是 0:否 -1:未知 | 
2.2 格式化时间
import time
  if __name__ == '__main__':     currentTime = time.localtime()          print("使用time.asctime:", time.asctime(currentTime))          print("使用time.strftime:", time.strftime("%Y-%m-%d %H:%M:%S", currentTime))     print("使用time.strftime:", time.strftime("%Y-%m-%d", currentTime))     
  使用time.asctime: Tue Aug 15 23:01:28 2023 使用time.strftime: 2023-08-15 23:01:28 使用time.strftime: 2023-08-15
 
  | 
 
1. 格式化符号说明
%y : 两位数的年份表示(00-99) 
%Y : 四位数的年份表示(000-9999) 
%m : 月份(01-12) 
%d : 月内中的一天(0-31) 
%H : 24小时制小时数(0-23) 
%I : 12小时制小时数(01-12) 
%M : 分钟数(00-59) 
%S : 秒(00-59) 
%a : 本地简化星期名称 
%A : 本地完整星期名称 
%b : 本地简化的月份名称 
%B : 本地完整的月份名称 
%c : 本地相应的日期表示和时间表示 
%j : 年内的一天(001-366) 
%p : 本地A.M.或P.M.的等价符 
%U : 一年中的星期数(00-53)星期天为星期的开始 
%w : 星期(0-6),星期天为星期的开始 
%W : 一年中的星期数(00-53)星期一为星期的开始 
%x : 本地相应的日期表示 
%X : 本地相应的时间表示 
%Z : 当前时区的名称 
%% : %号本身 
2.3 时间戳格式化
import time
  if __name__ == '__main__':     t = time.localtime(1692112041)     print("反解时间:", time.strftime("%Y-%m-%d %H:%M:%S", t))     
  反解时间: 2023-08-15 23:07:21
 
  | 
 
2.4 程序暂停
import time
  if __name__ == '__main__':     currentTime = time.time()     print("开始睡眠3秒: {} ".format(time.strftime("%Y-%m-%d %H:%M:%S")))     time.sleep(3)     print("睡眠后: {} ".format(time.strftime("%Y-%m-%d %H:%M:%S")))     endTime = time.time()     print("时间相差:", endTime - currentTime)     
  开始睡眠3秒: 2023-08-16 22:26:21  睡眠后: 2023-08-16 22:26:24  时间相差: 3.003920078277588
 
  | 
 
3. datetime使用
官网的对这个模块的介绍: 在支持日期时间数学运算的同时,实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作。
3.1 当前时间
print("当前时间: ", datetime.now())
 
 
  | 
 
3.2 时间戳互转
from datetime import datetime
  if __name__ == '__main__':     t = datetime(2023, 7, 16, 23, 40, 59)     print("获取指定时间: ", t)          tt = t.timestamp()     print("转成时间戳: ", t.timestamp())          print("时间戳反转: ", datetime.fromtimestamp(tt))     
  获取指定时间:  2023-07-16 23:40:59 转成时间戳:  1689522059.0 时间戳反转:  2023-07-16 23:40:59
 
  | 
 
3.3 str和datetime互转
from datetime import datetime
  if __name__ == '__main__':          strTime = "2022-02-22 22:22:22"     d = datetime.strptime(strTime, "%Y-%m-%d %H:%M:%S")     print("格式后时间: ", d)     print("格式后转时间戳: ", d.timestamp())     print("格式后类型: ", type(d))          print("datetime转字符串: ", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))     
  格式后时间:  2022-02-22 22:22:22 格式后转时间戳:  1645539742.0 格式后类型:  <class 'datetime.datetime'> datetime转字符串:  2023-08-16 23:47:24
 
  | 
 
3.4 时间计算
from datetime import datetime, timedelta
  if __name__ == '__main__':     currentTime = datetime.now()     print("当前时间: ", currentTime.strftime("%Y-%m-%d %H:%M:%S"))          print("3秒前: ", currentTime - timedelta(seconds=3))          print("5分钟前: ", currentTime - timedelta(minutes=5))          print("2小时前: ", currentTime - timedelta(hours=2))          print("3天前: ", currentTime - timedelta(days=3))          print("4周前: ", currentTime - timedelta(weeks=4))          
  当前时间:  2023-08-16 23:53:49 3秒前:  2023-08-16 23:53:46.581242 5分钟前:  2023-08-16 23:48:49.581242 2小时前:  2023-08-16 21:53:49.581242 3天前:  2023-08-13 23:53:49.581242 4周前:  2023-07-19 23:53:49.581242
 
  | 
 
@注: 往后求时间,则把currentTime - timedelta(n)中的减号,改成加号即可
4. calendar使用
这个库主要提供日历相关的实用函数
该库工作中使用的不是很频繁,直接列出几个常用的函数示例:
import calendar import datetime
  if __name__ == '__main__':          print("-------------------------指定日期星期几--------------------------")     print("2023-08-22 星期几?", calendar.weekday(2023, 8, 22) + 1)     print("-------------------------判断是否是闰年--------------------------")     print("判断2023是闰年吗?", calendar.isleap(2023))     print("-------------------------判断有多少闰年--------------------------")     print("判断2000-2023年有多少闰年?", calendar.leapdays(2000, 2023))
      print("-------------------------一个月的日历矩阵-------------------------")          print(calendar.monthcalendar(2023, 8))     print("-------------------------日期转换为时间戳--------------------------")          print(calendar.timegm(datetime.datetime(2023, 8, 22).timetuple()))
      print("-------------------------使用prmonth打印指定月日历------------------")          print(calendar.prmonth(2023, 8))     print("-------------------------使用month打印指定月日历--------------------")     print(calendar.month(2023, 8))     print("---------------------------打印整年日历----------------------------")          print(calendar.prcal(2023))
 
  | 
 
输出:
-------------------------指定日期星期几-------------------------- 2023-08-22 星期几? 2 -------------------------判断是否是闰年-------------------------- 判断2023是闰年吗? False -------------------------判断有多少闰年-------------------------- 判断2000-2023年有多少闰年? 6 -------------------------一个月的日历矩阵------------------------- [[0, 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, 0, 0, 0]] -------------------------日期转换为时间戳-------------------------- 1692662400 -------------------------打印指定月日历1---------------------------     August 2023 Mo Tu We Th Fr Sa Su     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 None -------------------------打印指定月日历2---------------------------     August 2023 Mo Tu We Th Fr Sa Su     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
  ---------------------------打印整年日历----------------------------                                   2023
        January                   February                   March Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su                    1             1  2  3  4  5             1  2  3  4  5  2  3  4  5  6  7  8       6  7  8  9 10 11 12       6  7  8  9 10 11 12  9 10 11 12 13 14 15      13 14 15 16 17 18 19      13 14 15 16 17 18 19 16 17 18 19 20 21 22      20 21 22 23 24 25 26      20 21 22 23 24 25 26 23 24 25 26 27 28 29      27 28                     27 28 29 30 31 30 31
         April                      May                       June Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su                 1  2       1  2  3  4  5  6  7                1  2  3  4  3  4  5  6  7  8  9       8  9 10 11 12 13 14       5  6  7  8  9 10 11 10 11 12 13 14 15 16      15 16 17 18 19 20 21      12 13 14 15 16 17 18 17 18 19 20 21 22 23      22 23 24 25 26 27 28      19 20 21 22 23 24 25 24 25 26 27 28 29 30      29 30 31                  26 27 28 29 30
          July                     August                  September Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su                 1  2          1  2  3  4  5  6                   1  2  3  3  4  5  6  7  8  9       7  8  9 10 11 12 13       4  5  6  7  8  9 10 10 11 12 13 14 15 16      14 15 16 17 18 19 20      11 12 13 14 15 16 17 17 18 19 20 21 22 23      21 22 23 24 25 26 27      18 19 20 21 22 23 24 24 25 26 27 28 29 30      28 29 30 31               25 26 27 28 29 30 31
        October                   November                  December Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su                    1             1  2  3  4  5                   1  2  3  2  3  4  5  6  7  8       6  7  8  9 10 11 12       4  5  6  7  8  9 10  9 10 11 12 13 14 15      13 14 15 16 17 18 19      11 12 13 14 15 16 17 16 17 18 19 20 21 22      20 21 22 23 24 25 26      18 19 20 21 22 23 24 23 24 25 26 27 28 29      27 28 29 30               25 26 27 28 29 30 31 30 31 None
 
  |