-
Notifications
You must be signed in to change notification settings - Fork 50
/
dates.py
37 lines (32 loc) · 1.54 KB
/
dates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
# @Author: jxing666
from datetime import datetime, timedelta
class DateUtils(object):
@staticmethod
def dateOperations(date=None, timedelta_kwargs=None):
"""
日期操作工具
:param date: datetime or date str
:param timedelta_kwargs: date operations kwargs
"""
if timedelta_kwargs and not isinstance(timedelta_kwargs, dict):
raise ValueError("参数错误")
if date:
if isinstance(date, str):
# 将时间字符串解析为日期对象
date = datetime.strptime(date, "%Y-%m-%d")
elif isinstance(date, datetime):
pass
else:
raise TypeError("日期类型错误")
else:
date = datetime.now()
new_date_after_addition = date + timedelta(**timedelta_kwargs)
return str(new_date_after_addition)[:19]
if __name__ == '__main__':
print(f"当前时间: {str(datetime.now())[:10]} +3天 = :", DateUtils.dateOperations(timedelta_kwargs={"days": 3}))
print(f"当前时间: {str(datetime.now())[:10]} -3天 = :", DateUtils.dateOperations(timedelta_kwargs={"days": -3}))
# 指定日期字符串
print(f"时间: 2023-11-01 +3天 = :", DateUtils.dateOperations("2023-11-01", timedelta_kwargs={"days": 3}))
print(f"时间: 2023-11-01 +3天 = :", DateUtils.dateOperations("2023-11-01", timedelta_kwargs={"days": -3}))
print(f"时间: 2023-11-01 +3天 = :", DateUtils.dateOperations("2023-11-01", timedelta_kwargs={"days": -3}))