又肝了3天,整理了80个Python DateTime 例子,必须收藏! (8)

Output:

Current Date Time: 2017-04-04 18:36:29.213046 Subtract 6 days: 2017-03-29 18:36:29.213046 Subtract 6 months: 2016-10-04 18:36:29.213046 Subtract 6 years: 2011-04-04 18:36:29.213046 Subtract 6 hours: 2017-04-04 12:36:29.213046 Subtract 6 mins: 2017-04-04 18:30:29.213046 Subtract 6 seconds: 2017-04-04 18:36:23.213046 复制代码 获取指定年份和月份的月份第一天的工作日和月份的天数 import calendar print("Year:2002 - Month:2") month_range = calendar.monthrange(2002, 2) print("Weekday of first day of the month:", month_range[0]) print("Number of days in month:", month_range[1]) print() print("Year:2010 - Month:5") month_range = calendar.monthrange(2010, 5) print("Weekday of first day of the month:", month_range[0]) print("Number of days in month:", month_range[1]) 复制代码

Output:

Year:2002 - Month:2 Weekday of first day of the month: 4 Number of days in month: 28 Year:2010 - Month:5 Weekday of first day of the month: 5 Number of days in month: 31 复制代码 打印特定年份的所有星期一 from datetime import date, timedelta year = 2018 date_object = date(year, 1, 1) date_object += timedelta(days=1-date_object.isoweekday()) while date_object.year == year: print(date_object) date_object += timedelta(days=7) 复制代码

Output:

2018-01-01 2018-01-08 2018-01-15 2018-01-22 2018-01-29 2018-02-05 2018-02-12 ... 2018-11-12 2018-11-19 2018-11-26 2018-12-03 2018-12-10 2018-12-17 2018-12-24 2018-12-31 复制代码 打印特定年份的日历 import calendar cal_display = calendar.TextCalendar(calendar.MONDAY) # Year: 2019 # Column width: 1 # Lines per week: 1 # Number of spaces between month columns: 0 # No. of months per column: 2 print(cal_display.formatyear(2019, 1, 1, 0, 2)) 复制代码

Output:

复制代码 从月份编号中获取月份名称 import calendar import datetime # Month name from number print("Month name from number 5:") month_num = 1 month_abre = datetime.date(2015, month_num, 1).strftime(\'%b\') month_name = datetime.date(2015, month_num, 1).strftime(\'%B\') print("Short Name:", month_abre) print("Full Name:", month_name) print("\nList of all months from calendar") # Print list of all months from calendar for month_val in range(1, 13): print(calendar.month_abbr[month_val], "-", calendar.month_name[month_val]) 复制代码

Output:

Month name from number 5: Short Name: Jan Full Name: January List of all months from calendar Jan - January Feb - February Mar - March Apr - April May - May Jun - June Jul - July Aug - August Sep - September Oct - October Nov - November Dec - December 复制代码 从给定日期获取一周的开始和结束日期 from datetime import datetime, timedelta date_str = \'2018-01-14\' date_obj = datetime.strptime(date_str, \'%Y-%m-%d\') start_of_week = date_obj - timedelta(days=date_obj.weekday()) # Monday end_of_week = start_of_week + timedelta(days=6) # Sunday print(start_of_week) print(end_of_week) 复制代码

Output:

2018-01-08 00:00:00 2018-01-14 00:00:00 复制代码 根据当前日期查找上一个和下一个星期一的日期 import datetime today = datetime.date.today() last_monday = today - datetime.timedelta(days=today.weekday()) coming_monday = today + datetime.timedelta(days=-today.weekday(), weeks=1) print("Today:", today) print("Last Monday:", last_monday) print("Coming Monday:", coming_monday) 复制代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzppdz.html