Output:
02/25/2020 08:26 AM 复制代码 查找从今天开始的第一个星期一的日期 from dateutil.rrule import rrule, WEEKLY, MO from datetime import date next_monday = rrule(freq=WEEKLY, dtstart=date.today(), byweekday=MO, count=1)[0] print(next_monday) 复制代码Output:
2021-05-17 00:00:00 复制代码 两个日期之间的差异(以天为单位) from datetime import date d1 = date(2019, 8, 18) d2 = date(2021, 12, 10) days_diff = d2 - d1 print(days_diff.days) 复制代码Output:
845 复制代码 向当前日期添加六个月 from datetime import datetime from dateutil.relativedelta import * date = datetime.now() print(date) date = date + relativedelta(months=+6) print(date) 复制代码Output:
2021-05-15 13:48:52.135612 2021-11-15 13:48:52.135612 复制代码 将数据时间对象转换为 Unix(时间戳) import datetime import time # Saturday, October 10, 2015 10:10:00 AM date_obj = datetime.datetime(2015, 10, 10, 10, 10) print("Unix Timestamp: ", (time.mktime(date_obj.timetuple()))) 复制代码Output:
Unix Timestamp: 1444452000.0 复制代码 将年、月、日、时、分、秒的 N 个数字添加到当前日期时间 import datetime from dateutil.relativedelta import relativedelta add_days = datetime.datetime.today() + relativedelta(days=+6) add_months = datetime.datetime.today() + relativedelta(months=+6) add_years = datetime.datetime.today() + relativedelta(years=+6) add_hours = datetime.datetime.today() + relativedelta(hours=+6) add_mins = datetime.datetime.today() + relativedelta(minutes=+6) add_seconds = datetime.datetime.today() + relativedelta(seconds=+6) print("Current Date Time:", datetime.datetime.today()) print("Add 6 days:", add_days) print("Add 6 months:", add_months) print("Add 6 years:", add_years) print("Add 6 hours:", add_hours) print("Add 6 mins:", add_mins) print("Add 6 seconds:", add_seconds) 复制代码Output:
Current Date Time: 2017-04-04 18:32:10.192671 Add 6 days: 2017-04-10 18:32:10.191671 Add 6 months: 2017-10-04 18:32:10.192671 Add 6 years: 2023-04-04 18:32:10.192671 Add 6 hours: 2017-04-05 00:32:10.192671 Add 6 mins: 2017-04-04 18:38:10.192671 Add 6 seconds: 2017-04-04 18:32:16.192671 复制代码 获取指定开始日期和结束日期之间的日期范围 import datetime start = datetime.datetime.strptime("2016-06-15", "%Y-%m-%d") end = datetime.datetime.strptime("2016-06-30", "%Y-%m-%d") date_array = \ (start + datetime.timedelta(days=x) for x in range(0, (end-start).days)) for date_object in date_array: print(date_object.strftime("%Y-%m-%d")) 复制代码Output:
2016-06-15 2016-06-16 2016-06-17 2016-06-18 2016-06-19 2016-06-20 2016-06-21 2016-06-22 2016-06-23 2016-06-24 2016-06-25 2016-06-26 2016-06-27 2016-06-28 2016-06-29 复制代码 减去 N 个年、月、日、时、分、秒到当前日期时间 import datetime from dateutil.relativedelta import relativedelta sub_days = datetime.datetime.today() + relativedelta(days=-6) sub_months = datetime.datetime.today() + relativedelta(months=-6) sub_years = datetime.datetime.today() + relativedelta(years=-6) sub_hours = datetime.datetime.today() + relativedelta(hours=-6) sub_mins = datetime.datetime.today() + relativedelta(minutes=-6) sub_seconds = datetime.datetime.today() + relativedelta(seconds=-6) print("Current Date Time:", datetime.datetime.today()) print("Subtract 6 days:", add_days) print("Subtract 6 months:", add_months) print("Subtract 6 years:", add_years) print("Subtract 6 hours:", add_hours) print("Subtract 6 mins:", add_mins) print("Subtract 6 seconds:", add_seconds) 复制代码