1.概述
Python中的日期不是其自身的數(shù)據(jù)類型,我們可以導(dǎo)入名為datetime的模塊,把日期當(dāng)做日期對象處理。
1.1 全部模塊
datetime包括以下類/函數(shù):

全部模塊
1.2 常用模塊
| 類名 | 功能說明 |
|---|---|
| date | 日期對象,常用的屬性有year, month, day |
| time | 時間對象 |
| datetime | 日期時間對象,常用的屬性有hour, minute, second, microsecond |
| timedelta | 時間間隔,即兩個時間點之間的長度 |
| tzinfo | 時區(qū)信息對象 |
1.3 常量
| 常量 | 功能說明 | 用法 | 返回值 |
|---|---|---|---|
| MAXYEAR | 返回能表示的最大年份 | datetime.MAXYEAR | 9999 |
| MINYEAR | 返回能表示的最小年份 | datetime.MINYEAR | 1 |
datetime.py文件中有如下定義:
MINYEAR = 1
MAXYEAR = 9999
_MAXORDINAL = 3652059 # date.max.toordinal()
2. 模塊詳解
2.1 datetime
datetime模塊初始化代碼如下:
class datetime(date):
"""datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
The year, month and day arguments are required. tzinfo may be None, or an
instance of a tzinfo subclass. The remaining arguments may be ints.
"""
__slots__ = date.__slots__ + time.__slots__
def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None, *, fold=0):
示例:
# 導(dǎo)入日期模塊
import datetime
# 顯示當(dāng)前日期:2019-08-14 12:52:55.817273
x = datetime.datetime.now()
print(x)
# 創(chuàng)建日期對象:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
# 顯示月份的名稱:
import datetime
x = datetime.datetime(2019, 10, 1)
print(x.strftime("%B"))
所有合法格式代碼的參考:
| 指令 | 描述 | 實例 |
|---|---|---|
| %a | Weekday,短版本 | Wed |
| %A | Weekday,完整版本 | Wednesday |
| %w | Weekday,數(shù)字 0-6,0 為周日 | 3 |
| %d | 日,數(shù)字 01-31 | 31 |
| %b | 月名稱,短版本 | Dec |
| %B | 月名稱,完整版本 | December |
| %m | 月,數(shù)字01-12 | 12 |
| %y | 年,短版本,無世紀(jì) | 18 |
| %Y | 年,完整版本 | 2018 |
| %H | 小時,00-23 | 17 |
| %I | 小時,00-12 | 05 |
| %p | AM/PM | PM |
| %M | 分,00-59 | 41 |
| %S | 秒,00-59 | 08 |
| %f | 微妙,000000-999999 | 548513 |
| %z | UTC 偏移 | +0100 |
| %Z | 時區(qū) | CST |
| %j | 天數(shù),001-366 | 365 |
| %U | 周數(shù),每周的第一天是周日,00-53 | 52 |
| %W | 周數(shù),每周的第一天是周一,00-53 | 52 |
| %c | 日期和時間的本地版本 | Mon Dec 31 17:41:00 2018 |
| %x | 日期的本地版本 | 12/31/18 |
| %X | 時間的本地版本 | 17:41:00 |
| %% | A % character | % |
2.2 timedelta
timedelta類是用來計算二個datetime對象的差值。
此類中包含如下屬性:
1、days:天數(shù)
2、microseconds:微秒數(shù)(>=0 并且 <1秒)
3、seconds:秒數(shù)(>=0 并且 <1天)
初始化代碼如下:
class timedelta:
"""Represent the difference between two datetime objects.
Supported operators:
- add, subtract timedelta
- unary plus, minus, abs
- compare to timedelta
- multiply, divide by int
In addition, datetime supports subtraction of two datetime objects
returning a timedelta, and addition or subtraction of a datetime
and a timedelta giving a datetime.
Representation: (days, seconds, microseconds). Why? Because I
felt like it.
"""
__slots__ = '_days', '_seconds', '_microseconds', '_hashcode'
def __new__(cls, days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0):
# Doing this efficiently and accurately in C is going to
示例:
# 可用變量單位:days=0, seconds=0, microseconds=0,milliseconds=0, minutes=0, hours=0, weeks=0
# Out[37]: datetime.datetime(2020, 9, 29, 10, 49, 38, 755711)
datetime.datetime.today() + datetime.timedelta(days=-1)
# Out[36]: datetime.datetime(2020, 9, 30, 9, 48, 42, 363677)
datetime.datetime.today() + datetime.timedelta(hours=-1)