Python日期时间datetime库与dateutil库典型示例与详解(5)--dateutil库简介&dateutil库relativedelta类常用例子

一、dateutil库简介

1、dateutil库需结合datetime库使用,因为dateutil库基础的与时间相关的数据结构都是用的datetime库的datetime类、date类

2、dateutil库最多应用的是relativedelta类和rrule类:

(1)relativedelta类是更丰富的时间间隔计算、时间生成工具,让我们不用考虑大小月,闰年等因素;

(2)rrule类能依据各种复杂规则(频率、间隔等),在指定的时间段内生成我们需要的时间对象列表

本文示例的引用方式为:

from dateutil.relativedelta import *
from dateutil.rrule import *

关于python的import和名字空间,请参考之前文章:

《Python日期时间datetime库与dateutil库典型示例与详解(1)--datetime库简介&datetime库date类常用例子》

二、dateutil库relativedelta类常用例子

dateutil库relativedelta类是datetime库timedelta类(timedelta只有days、seconds、microseconds三个单位的时间间隔)的加强版,relativedelta类可使用的时间单位更多,让我们不用考虑大小月,闰年等因素:

1、生成relativedelta对象

通常生成relativedelta对象有两种方法:

(1)用通过各种时间单位单个或者组合的方式生成relativedelta对象,需要注意relativedelta类

myrelativedelta = relativedelta (

    years=1,

months =1,

weeks =1,

    days =2,

    hours =2,

    minutes =1,

    seconds =30,

microseconds =2)

各时间单位只要是正数就有效,会自动进位,例如:

relativedelta (years=1,months =13)等于relativedelta (years=2,months =1)

注意weeks会自动转换成days,relativedelta不会保存weeks时间单位的值

(2)两个datetime、date对象做参数

Mydate1 = date(2023,2,1)
Mydate2 = date(
2023,12,16)

Mydatetime1 = datetime(2023,11,16,12,12,12,999)
Mydatetime2 = datetime.today()

#两个date对象

Myrelativedelta1 = relativedelta (Mydate1, Mydate2)

#两个datetime对象

Myrelativedelta2 = relativedelta (Mydatetime1, Mydatetime2)

#date对象和datetime对象之间也可以

Myrelativedelta3 = relativedelta (Mydate1, Mydatetime1)

2、relativedelta对象的字段值

relativedelta对象仅保存有以下字段值供我们获取使用,没有weeks(都是int类型):

    years/months/days/hours/minutes/seconds/microseconds

当遇到两个跨度较长的date对象,我们不用考虑大小月,闰年等因素用relativedelta就能知道他们相差x年x月

      对比之前介绍datetime库的timedelta类,timedelta对象只有“days/seconds/microseconds”单位的时间差,relativedelta确实功能强大些。

      当然datetime库的timedelta类也不是完全没有适用场景,也许有时候我们就不想要什么两个日期对象相隔的“年、月”单位,我就只想要‘天’的单位,那这个时候就应该用datetime库的timedelta了。

3、relativedelta对象单数/复数时间单位的用法

用时间单位生成relativedelta对象时,有复数和单数(如:years/year、days/day)两种形式,复数代表相对值,单数代表绝对值。

(1)relativedelta对象单数的时间单位的值与date/datetime对象做加价运算时,用于date/datetime对象同样时间单位的值做赋值操作,类似与date/datetime自己的replace()方法:

例如:

Mydatetime1 = datetime(2023,11,16,10,11,11,000111)

Myrelativedelta1 = relativedelta(day=2,hour=8,minute=2)

Mydatetime2 = Mydatetime1 + Myrelativedelta1

# Mydatetime2的值为 (2023,11,2,8,2,11,000111),

(2)relativedelta对象复数的时间单位的值与date/datetime对象做加运算时,用于date/datetime对象同样时间单位的值做加法操作:

例如:

Mydatetime1 = datetime(2023,11,16,10,11,11,000111)

Myrelativedelta1 = relativedelta(days=2,hours=8,minutes=2)

Mydatetime2 = Mydatetime1 + Myrelativedelta1

# Mydatetime2的值为 (2023,11,18,18,13,11,000111),

(3)relativedelta对象单数和复数的时间单位都有值与date/datetime对象做加运算时,用于date/datetime对象同样时间单位,先用单数值赋值,再用复数值做法操作:

Mydatetime1 = datetime(2023,11,16,10,11,11,000111)

Myrelativedelta1 = relativedelta(days=2,day=2,hour=8)

Mydatetime2 = Mydatetime1 + Myrelativedelta1

# Mydatetime2的值为 (2023,11,4,18,11,11,000111),

4、求date对象相差多少年月日

Mydate1 = date(2023,2,1)
Mydate2 = date(
1988,12,16)

#两个date对象

Myrelativedelta1 = relativedelta (Mydate1, Mydate2)

years = Myrelativedelta1.years

months = Myrelativedelta1.months

days = Myrelativedelta1.days

5、指定时间间隔生成新的date/datetime对象

Mydatetime1 = datetime.today()

Mydatetime2 = Mydatetime1 + relativedelta(months=2,days=3)

6、当前日期2天后10点生成datetime对象

需要同时用到relativedelta对象单数和复数的时间单位,2天后是个相对值用days,10点是个绝对值:

Mydatetime1 = datetime.today()

#假如Mydatetime1为 (2023,11,16,8,11,11,000111)

Myrelativedelta1 = relativedelta(days=2,hour=10)

Mydatetime2 = Mydatetime1 + Myrelativedelta1

# Mydatetime2的值为 (2023,11,18,10,11,11,000111)

Mydatetime2不符合我们要求,应该是:

Myrelativedelta2 = relativedelta(days=2,hour=10,minute=0,second=0.microsecond=0)

Mydatetime3 = Mydatetime1 + Myrelativedelta2

# Mydatetime3的值为 (2023,11,18,10,0,0,0)

或者:

Mydate = date.today()

Myrelativedelta1 = relativedelta(days=2,hour=10)

Mydate2= Mydate + Myrelativedelta1

当然,我们这个场景完全没必要这么复杂只用datetime库就可以了:

today = date.today()

mydatetime = datetime(today.year,today.month,today.day+2,10)

或者

mydatetime = datetime.combine(date.today()+timedelta(days=2),time(10))

或者

mydatetime = (datetime.today()+timedelta(days=2)).replace(

hour=10,minute=0,second =0,microsecond=0)

7、求某日期前后的第N个星期N

MO, TU, WE, TH, FR, SA, SU 分别代表:周一到周天

(1)今天以后第的第1个星期6

next_SA = date.today() + relativedelta(weekday= SA)

(2)2022年的倒数第2个星期三

Last_we = date(2022,12,31) + relativedelta(weekday= WE(-2))

MO, TU, WE, TH, FR, SA, SU可以带()用,负号表示指定日期往前,什么都不带SA=SA(+1)

你可能感兴趣的:(python基础,python,开发语言)