核心功能实现
1. 基础时间获取
1@staticmethod
2def get_current_date() -> date:
3 """获取当前日期对象(date类型)[1][3]
4 Example: 2024-02-15
5 """
6 return date.today()
7
8@staticmethod
9def get_current_datetime() -> datetime:
10 """获取当前日期时间对象(datetime类型)[2][3]
11 Example: 2024-02-15 14:30:45.123456
12 """
13 return datetime.now()
2. 日期格式化与解析
1@staticmethod
2def format_date(dt: date, fmt: str = "%Y-%m-%d") -> str:
3 """日期格式化输出[1][2]
4 Example: 2024-02-15 → "15/02/2024"
5 """
6 return dt.strftime(fmt)
7
8@staticmethod
9def parse_date_str(date_str: str, fmt: str = "%Y-%m-%d") -> date:
10 """字符串转日期对象[2][3]
11 Example: "20240215" → 2024-02-15
12 """
13 return datetime.strptime(date_str, fmt).date()
3. 日期计算功能
1@staticmethod
2def calculate_days_diff(start: date, end: date) -> int:
3 """计算两个日期之间的天数差[2]
4 Example: 2024-02-10与2024-02-15 → 5天
5 """
6 return (end - start).days
7
8@staticmethod
9def add_days(base_date: date, days: int) -> date:
10 """日期加减计算[1][3]
11 Example: 2024-02-15 + 3天 → 2024-02-18
12 """
13 return base_date + timedelta(days=days)
4. 日历相关功能
1@staticmethod
2def get_month_calendar(year: int, month: int) -> str:
3 """生成月份日历[4][5]
4 Example: 2024年2月 → 返回格式化日历表格
5 """
6 return calendar.month(year, month)
7
8@staticmethod
9def is_month_end(dt: date) -> bool:
10 """判断是否月末[4][5]
11 Example: 2024-02-15 → False
12 """
13 return dt.day == calendar.monthrange(dt.year, dt.month)[1]
5. 高级判断功能
1@staticmethod
2def is_future_date(target: date) -> bool:
3 """判断是否为未来日期[1]
4 Example: 2025-01-01 → True
5 """
6 return target > date.today()
7
8@staticmethod
9def get_workdays(year: int, month: int) -> List[date]:
10 """获取当月工作日列表[4][5]
11 Example: 2024年2月 → 过滤周末的日期列表
12 """
13 return [date(year, month, day) for day in range(1, calendar.monthrange(year, month)[1]+1)
14 if date(year, month, day).weekday() < 5]
6. 时间维度计算
1@staticmethod
2def get_week_number(dt: date) -> int:
3 """获取ISO周数[2][3]
4 Example: 2024-02-15 → 周数7
5 """
6 return dt.isocalendar()[1]
7
8@staticmethod
9def get_age(birth_date: date) -> int:
10 """精确年龄计算[2][3]
11 Example: 2000-03-15 → 24岁(当前日期2024-02-15)
12 """
13 today = date.today()
14 return today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
使用示例
1print("当前日期:", DateUtils.get_current_date()) # [1][3]
2print("月末判断:", DateUtils.is_month_end(date(2024,2,28))) # [4][5]
3print("工作日列表:", DateUtils.get_workdays(2024, 2)) # [4][5]
功能对比表
| 功能 | 核心方法 | 相关模块 | 应用场景 |
|---|---|---|---|
| 日期格式化 | strftime | datetime | 日志记录 |
| 工作日计算 | weekday + calendar | calendar | 考勤统计 |
| 闰年判断 | monthrange | calendar | 日期校验 |
| 时区转换 | astimezone | dateutil | 跨国系统 |
最佳实践建议
- 处理跨时区场景时建议统一使用UTC时间[1][3]
- 日期比较前确保时区一致性[1][4]
- 使用calendar.monthrange()处理月末日期更可靠[4][5]
- 批量处理日期时建议先进行时区转换[3][4]
该工具类已涵盖日常开发中常见的日期处理需求,通过合理组合使用这些方法,可以显著提升开发效率。
参考资料
- Python
datetime官方文档:https://docs.python.org/3/library/datetime.html - Python
date/timedelta用法:https://docs.python.org/3/library/datetime.html#date-objects - Python
zoneinfo时区文档:https://docs.python.org/3/library/zoneinfo.html - Python
calendar官方文档:https://docs.python.org/3/library/calendar.html python-dateutil项目文档:https://dateutil.readthedocs.io/