![](https://aitechtogether.com/wp-content/uploads_new/2023/12/77096d5a-7b38-49eb-bd0f-4f11c5702324.webp)
标准库
datetime
>>> import datetime
>>> date = datetime.date(2023, 12, 20)
>>> print(date)
2023-12-20
>>> date = datetime.datetime(2023, 12, 20)
>>> print(date)
2023-12-20 00:00:00
>>> print(date.strftime(‘%Y-%m-%d’))
2023-12-20
>>> today = datetime.date.today()
>>> print(today)
2023-12-20
>>> today.strftime(‘%A’)
‘Wednesday’
>>> today.weekday()
2 # 0代表星期天,1代表星期一,以此类推
>>> today.isoweekday()
3 # 1代表星期一,以此类推,7代表星期天
>>> datetime.date(2023, 12, 24).weekday()
6
>>> datetime.date(2023, 12, 24).isoweekday()
7
dateutil
>>> from dateutil import parser
>>> date = ‘2023-12-20’
>>> parser.parse(date)
datetime.datetime(2023, 12, 20, 0, 0)
>>> parser.parse(date).strftime(‘%Y-%m-%d’)
‘2023-12-20’
>>> parser.parse(date).strftime(‘%A’)
‘Wednesday’
>>> parser.parse(date).weekday()
2
calendar
>>> import calendar
>>> calendar.weekday(2023, 12, 20)
2
附: 日期时间常用的格式
%Y
:四位数的年份(例如2023)%y
:两位数的年份(例如23)%m
:两位数的月份(01到12)%d
:两位数的日期(01到31)%H
:24小时制的小时数(00到23)%I
:12小时制的小时数(01到12)%M
:两位数的分钟数(00到59)%S
:两位数的秒数(00到59)%f
:微秒数(000000到999999)%p
:AM或PM%A
:完整的星期名称(例如Monday星期一)%a
:简写的星期名称(例如Mon星期一)%B
:完整的月份名称(例如January一月)%b
:简写的月份名称(例如Jan一月)%c
:日期和时间,使用系统默认的格式%x
:日期,使用系统默认的格式%X
:时间,使用系统默认的格式%Z
:时区名称或缩写
第三方库
arrow
安装: pip install arrow
>>> import arrow
>>> arrow.Arrow(2023, 12, 20)
<Arrow [2023-12-20T00:00:00+00:00]>
>>> arrow.Arrow(2023, 12, 20, 20, 30)
<Arrow [2023-12-20T20:30:00+00:00]>
>>> arrow.utcnow()
<Arrow [2023-12-20T13:03:30.505669+00:00]>
>>> arrow.utcnow().ctime()
‘Wed Dec 20 13:03:48 2023’
>>> arrow.utcnow().date()
datetime.date(2023, 12, 20)
>>> arw = arrow.utcnow()
>>> arw.dehumanize(“2 days ago”)
<Arrow [2023-12-18T13:04:54.443942+00:00]>
>>> import arrow
>>> arrow.Arrow(2023, 12, 20)
<Arrow [2023-12-20T00:00:00+00:00]>
>>> arrow.Arrow(2023, 12, 20, 20, 30)
<Arrow [2023-12-20T20:30:00+00:00]>
>>> arw = arrow.utcnow()
>>> arw
<Arrow [2023-12-20T13:05:46.025519+00:00]>
>>> arw.dehumanize(“2 days ago”)
<Arrow [2023-12-18T13:05:46.025519+00:00]>
>>> arw.dehumanize(“in a month”)
<Arrow [2024-01-20T13:05:46.025519+00:00]>
>>> arw.format(‘YYYY-MM-DD’)
‘2023-12-20’
>>> arw.format(‘YYYY-MM-DD HH:mm:ss ZZ’)
‘2023-12-20 13:05:46 +00:00’
>>> arw.format()
‘2023-12-20 13:05:46+00:00’
>>> arw.format(‘YYYY-MM-DD HH:mm’)
‘2023-12-20 13:05’
>>> arw.isocalendar()
datetime.IsoCalendarDate(year=2023, week=51, weekday=3)
>>> arw.isoformat()
‘2023-12-20T13:05:46.025519+00:00’
>>> arw.isoweekday()
3
>>> arw.weekday()
2
>>> arw.replace(year=2024, month=6)
<Arrow [2024-06-20T13:05:46.025519+00:00]>
>>> arw.replace(year=2024, month=6).format(‘YYYY-MM-DD’)
‘2024-06-20’
>>> arw.shift(days=4)
<Arrow [2023-12-24T13:05:46.025519+00:00]>
>>> arw.shift(days=-2)
<Arrow [2023-12-18T13:05:46.025519+00:00]>
>>> arw.time()
datetime.time(13, 5, 46, 25519)
>>> arw.timetuple()
time.struct_time(tm_year=2023, tm_mon=12, tm_mday=20, tm_hour=13, tm_min=5, tm_sec=46, tm_wday=2, tm_yday=354, tm_isdst=0)
时间段分割 .range()
>>> import arrow
>>> start = arrow.Arrow(2023, 12, 10, 12, 30)
>>> end = arrow.Arrow(2023, 12, 20, 6, 30)
>>> for r in arrow.Arrow.range(‘day’, start, end):
print(r)
2023-12-10T12:30:00+00:00
2023-12-11T12:30:00+00:00
2023-12-12T12:30:00+00:00
2023-12-13T12:30:00+00:00
2023-12-14T12:30:00+00:00
2023-12-15T12:30:00+00:00
2023-12-16T12:30:00+00:00
2023-12-17T12:30:00+00:00
2023-12-18T12:30:00+00:00
2023-12-19T12:30:00+00:00
>>> end = arrow.Arrow(2023, 12, 12, 6, 30)
>>> for r in arrow.Arrow.range(‘hour’, start, end):
print(r)
2023-12-10T12:30:00+00:00
2023-12-10T13:30:00+00:00
2023-12-10T14:30:00+00:00
2023-12-10T15:30:00+00:00
2023-12-10T16:30:00+00:00
2023-12-10T17:30:00+00:00
2023-12-10T18:30:00+00:00
2023-12-10T19:30:00+00:00
2023-12-10T20:30:00+00:00
2023-12-10T21:30:00+00:00
2023-12-10T22:30:00+00:00
2023-12-10T23:30:00+00:00
2023-12-11T00:30:00+00:00
2023-12-11T01:30:00+00:00
2023-12-11T02:30:00+00:00
2023-12-11T03:30:00+00:00
2023-12-11T04:30:00+00:00
2023-12-11T05:30:00+00:00
2023-12-11T06:30:00+00:00
2023-12-11T07:30:00+00:00
2023-12-11T08:30:00+00:00
2023-12-11T09:30:00+00:00
2023-12-11T10:30:00+00:00
2023-12-11T11:30:00+00:00
2023-12-11T12:30:00+00:00
2023-12-11T13:30:00+00:00
2023-12-11T14:30:00+00:00
2023-12-11T15:30:00+00:00
2023-12-11T16:30:00+00:00
2023-12-11T17:30:00+00:00
2023-12-11T18:30:00+00:00
2023-12-11T19:30:00+00:00
2023-12-11T20:30:00+00:00
2023-12-11T21:30:00+00:00
2023-12-11T22:30:00+00:00
2023-12-11T23:30:00+00:00
2023-12-12T00:30:00+00:00
2023-12-12T01:30:00+00:00
2023-12-12T02:30:00+00:00
2023-12-12T03:30:00+00:00
2023-12-12T04:30:00+00:00
2023-12-12T05:30:00+00:00
2023-12-12T06:30:00+00:00
pendulum
还有一个时间处理库 ,内容更加丰富有空再研究研究。
NAME
pendulum
PACKAGE CONTENTS
__version__
_helpers
_pendulum
constants
date
datetime
day
duration
exceptions
formatting (package)
helpers
interval
locales (package)
mixins (package)
parser
parsing (package)
testing (package)
time
tz (package)
utils (package)
CLASSES
builtins.object
pendulum.formatting.formatter.Formatter
datetime.date(builtins.object)
pendulum.date.Date(pendulum.mixins.default.FormattableMixin, datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
datetime.datetime(datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
datetime.time(builtins.object)
pendulum.time.Time(pendulum.mixins.default.FormattableMixin, datetime.time)
datetime.timedelta(builtins.object)
pendulum.duration.Duration
pendulum.interval.Interval
datetime.tzinfo(builtins.object)
pendulum.tz.timezone.FixedTimezone(datetime.tzinfo, pendulum.tz.timezone.PendulumTimezone)
enum.IntEnum(builtins.int, enum.ReprEnum)
pendulum.day.WeekDay
pendulum.mixins.default.FormattableMixin(builtins.object)
pendulum.date.Date(pendulum.mixins.default.FormattableMixin, datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
pendulum.time.Time(pendulum.mixins.default.FormattableMixin, datetime.time)
pendulum.tz.timezone.PendulumTimezone(abc.ABC)
pendulum.tz.timezone.FixedTimezone(datetime.tzinfo, pendulum.tz.timezone.PendulumTimezone)
pendulum.tz.timezone.Timezone(zoneinfo.ZoneInfo, pendulum.tz.timezone.PendulumTimezone)
zoneinfo.ZoneInfo(datetime.tzinfo)
pendulum.tz.timezone.Timezone(zoneinfo.ZoneInfo, pendulum.tz.timezone.PendulumTimezone)
文章出处登录后可见!