05. date 类 - 处理日期 概述 date类是 datetime 模块中专门处理日期 (年、月、日)的类,不包含时间和时区信息。它是日常日期操作中最常用的类之一。
维度 内容 What 处理日期(年、月、日)的类 Why 专门处理日历日期,不关心具体时间 When 生日、节假日、日期范围、日历计算 Where from datetime import dateWho 需要处理日期而非时间的开发者 How d = date(2026, 4, 19)
1. date 类基础 1.1 创建 date 对象 from datetimeimport dateimport time# 方式1:直接指定年月日 d1= date( 2026 , 4 , 19 ) print ( d1) # 2026-04-19 # 方式2:获取当前日期 today= date. today( ) print ( today) # 2026-04-19 # 方式3:从时间戳创建 timestamp= time. time( ) d2= date. fromtimestamp( timestamp) print ( d2) # 2026-04-19 # 方式4:从 ISO 格式字符串创建(Python 3.7+) d3= date. fromisoformat( "2026-04-19" ) print ( d3) # 2026-04-19 # 方式5:从序数创建(公元1年1月1日为1) d4= date. fromordinal( 738264 ) print ( d4) # 2026-04-19 1.2 最小和最大日期 from datetimeimport date# 最小日期(year=1, month=1, day=1) min_date= date. min print ( f"最小日期: { min_date} " ) # 0001-01-01 # 最大日期(year=9999, month=12, day=31) max_date= date. max print ( f"最大日期: { max_date} " ) # 9999-12-31 # 最小时间间隔 resolution= date. resolutionprint ( f"分辨率: { resolution} " ) # 1 day, 0:00:00 2. date 属性 from datetimeimport date d= date( 2026 , 4 , 19 ) print ( f"年份: { d. year} " ) # 2026 print ( f"月份: { d. month} " ) # 4 print ( f"日期: { d. day} " ) # 19 3. date 实例方法 3.1 replace() - 替换部分字段 from datetimeimport date d= date( 2026 , 4 , 19 ) # 替换年份 print ( d. replace( year= 2027 ) ) # 2027-04-19 # 替换月份 print ( d. replace( month= 12 ) ) # 2026-12-19 # 替换日期 print ( d. replace( day= 1 ) ) # 2026-04-01 # 同时替换多个 print ( d. replace( year= 2025 , month= 5 , day= 10 ) ) # 2025-05-10 3.2 timetuple() - 转换为时间元组 from datetimeimport date d= date( 2026 , 4 , 19 ) t= d. timetuple( ) print ( f"时间元组: { t} " ) # time.struct_time(tm_year=2026, tm_mon=4, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=109, tm_isdst=-1) print ( f"年: { t. tm_year} " ) print ( f"月: { t. tm_mon} " ) print ( f"日: { t. tm_mday} " ) print ( f"星期(0=周一): { t. tm_wday} " ) print ( f"年中的第几天: { t. tm_yday} " ) 3.3 toordinal() - 转换为序数 from datetimeimport date d= date( 2026 , 4 , 19 ) ordinal= d. toordinal( ) print ( f"序数: { ordinal} " ) # 从公元1年1月1日开始的天数 # 从序数恢复 d2= date. fromordinal( ordinal) print ( f"恢复: { d2} " ) # 2026-04-19 3.4 weekday() 和 isoweekday() - 获取星期 from datetimeimport date d= date( 2026 , 4 , 19 ) # 周日 # weekday(): 周一=0, 周日=6 print ( f"weekday(): { d. weekday( ) } " ) # 6 # isoweekday(): 周一=1, 周日=7 print ( f"isoweekday(): { d. isoweekday( ) } " ) # 7 # 获取星期名称 weekdays= [ "周一" , "周二" , "周三" , "周四" , "周五" , "周六" , "周日" ] print ( f"星期: { weekdays[ d. weekday( ) ] } " ) 3.5 isoformat() - ISO 格式字符串 from datetimeimport date d= date( 2026 , 4 , 19 ) print ( d. isoformat( ) ) # 2026-04-19 3.6 ctime() - C 语言格式字符串 from datetimeimport date d= date( 2026 , 4 , 19 ) print ( d. ctime( ) ) # Sun Apr 19 00:00:00 2026 3.7 strftime() - 自定义格式化 from datetimeimport date d= date( 2026 , 4 , 19 ) print ( d. strftime( "%Y年%m月%d日" ) ) # 2026年04月19日 print ( d. strftime( "%Y-%m-%d" ) ) # 2026-04-19 print ( d. strftime( "%Y/%m/%d" ) ) # 2026/04/19 print ( d. strftime( "%d-%m-%Y" ) ) # 19-04-2026 print ( d. strftime( "%b %d, %Y" ) ) # Apr 19, 2026 print ( d. strftime( "%A" ) ) # Sunday 4. date 类方法 4.1 today() - 获取当前日期 from datetimeimport date today= date. today( ) print ( f"今天: { today} " ) 4.2 fromtimestamp() - 从时间戳创建 from datetimeimport dateimport time timestamp= time. time( ) d= date. fromtimestamp( timestamp) print ( d) 4.3 fromisoformat() - 从 ISO 字符串创建 from datetimeimport date d= date. fromisoformat( "2026-04-19" ) print ( d) # 2026-04-19 4.4 fromordinal() - 从序数创建 from datetimeimport date# 公元1年1月1日是第1天 d= date. fromordinal( 1 ) print ( d) # 0001-01-01 5. date 运算 5.1 日期加减 from datetimeimport date, timedelta today= date( 2026 , 4 , 19 ) # 加法 tomorrow= today+ timedelta( days= 1 ) yesterday= today- timedelta( days= 1 ) next_week= today+ timedelta( weeks= 1 ) last_month= today- timedelta( days= 30 ) print ( f"今天: { today} " ) print ( f"明天: { tomorrow} " ) print ( f"昨天: { yesterday} " ) print ( f"下周: { next_week} " ) print ( f"30天前: { last_month} " ) 5.2 日期差值 from datetimeimport date start= date( 2026 , 1 , 1 ) end= date( 2026 , 12 , 31 ) diff= end- startprint ( f"天数差: { diff. days} " ) # 364 5.3 日期比较 from datetimeimport date d1= date( 2026 , 4 , 19 ) d2= date( 2026 , 4 , 20 ) d3= date( 2026 , 4 , 19 ) print ( f"d1 < d2: { d1< d2} " ) # True print ( f"d1 > d2: { d1> d2} " ) # False print ( f"d1 == d3: { d1== d3} " ) # True 6. 实用示例 6.1 判断闰年 from datetimeimport datedef is_leap_year ( year) : """判断是否为闰年""" return year% 4 == 0 and ( year% 100 != 0 or year% 400 == 0 ) # 测试 years= [ 2024 , 2025 , 2026 , 2100 , 2400 ] for yin years: print ( f" { y} 年是闰年: { is_leap_year( y) } " ) 6.2 获取月份天数 from datetimeimport datefrom calendarimport monthrangedef get_month_days ( year, month) : """获取月份的天数""" return monthrange( year, month) [ 1 ] print ( f"2026年4月有 { get_month_days( 2026 , 4 ) } 天" ) 6.3 日期范围遍历 from datetimeimport date, timedelta start= date( 2026 , 4 , 1 ) end= date( 2026 , 4 , 7 ) current= startwhile current<= end: print ( current. strftime( "%Y-%m-%d %A" ) ) current+= timedelta( days= 1 ) 6.4 计算两个日期之间的天数 from datetimeimport datedef days_between ( d1, d2) : """计算两个日期之间的天数(绝对值)""" return abs ( ( d2- d1) . days) start= date( 2026 , 1 , 1 ) end= date( 2026 , 12 , 31 ) print ( f"2026年天数: { days_between( start, end) } " ) # 364 6.5 获取本月第一天和最后一天 from datetimeimport datefrom calendarimport monthrangedef get_month_first_last ( year, month) : """获取月份的第一天和最后一天""" first_day= date( year, month, 1 ) last_day= date( year, month, monthrange( year, month) [ 1 ] ) return first_day, last_day first, last= get_month_first_last( 2026 , 4 ) print ( f"4月第一天: { first} " ) print ( f"4月最后一天: { last} " ) 6.6 计算年龄 from datetimeimport datedef calculate_age ( birth_date) : """计算年龄""" today= date. today( ) age= today. year- birth_date. year# 如果今年生日还没过,减1 if ( today. month, today. day) < ( birth_date. month, birth_date. day) : age-= 1 return age birth= date( 1990 , 5 , 15 ) print ( f"年龄: { calculate_age( birth) } " ) 7. 完整示例 from datetimeimport date, timedeltaprint ( "=" * 50 ) print ( "date 类综合示例" ) print ( "=" * 50 ) # 1. 创建日期对象 print ( "\n1. 创建日期对象" ) d1= date( 2026 , 4 , 19 ) d2= date. today( ) print ( f"指定日期: { d1} " ) print ( f"当前日期: { d2} " ) # 2. 日期信息 print ( "\n2. 日期信息" ) print ( f"年份: { d1. year} " ) print ( f"月份: { d1. month} " ) print ( f"日期: { d1. day} " ) print ( f"星期: { d1. strftime( '%A' ) } " ) print ( f"年中第 { d1. timetuple( ) . tm_yday} 天" ) # 3. 日期运算 print ( "\n3. 日期运算" ) today= date. today( ) tomorrow= today+ timedelta( days= 1 ) yesterday= today- timedelta( days= 1 ) next_week= today+ timedelta( weeks= 1 ) print ( f"今天: { today} " ) print ( f"明天: { tomorrow} " ) print ( f"昨天: { yesterday} " ) print ( f"下周: { next_week} " ) # 4. 日期格式化 print ( "\n4. 日期格式化" ) print ( f"ISO格式: { today. isoformat( ) } " ) print ( f"中文格式: { today. strftime( '%Y年%m月%d日' ) } " ) print ( f"英文格式: { today. strftime( '%b %d, %Y' ) } " ) # 5. 日期比较 print ( "\n5. 日期比较" ) d1= date( 2026 , 4 , 19 ) d2= date( 2026 , 5 , 1 ) print ( f" { d1} < { d2} : { d1< d2} " ) print ( f" { d1} > { d2} : { d1> d2} " ) print ( f"相差天数: { ( d2- d1) . days} 天" ) # 6. 实用功能 print ( "\n6. 实用功能" ) birth= date( 1990 , 4 , 19 ) age= calculate_age( birth) print ( f"生日: { birth} " ) print ( f"年龄: { age} 岁" ) # 判断是否闰年 year= 2024 print ( f" { year} 年是闰年: { year% 4 == 0 and ( year% 100 != 0 or year% 400 == 0 ) } " ) 8. 常见陷阱 陷阱 说明 解决方案 无效日期 date(2026, 2, 29)会报错使用monthrange检查 月份范围 月份必须是 1-12 验证输入 日期范围 日期必须在有效范围内 验证输入 时区忽略 date 不包含时区信息 使用 datetime 处理时区
9. 总结 方法/属性 用途 date(year, month, day)创建日期对象 date.today()获取当前日期 year,month,day获取年月日 weekday()获取星期(0=周一) isoweekday()获取星期(1=周一) replace()替换部分字段 strftime()格式化输出 +/- timedelta日期加减 date1 - date2日期差值