Python|仅需6道题轻松掌握Python时间和日期处理 | Python技能树征题
仅需6道题轻松掌握Python时间和日期处理 | Python技能树征题
-
- 0.前言
- 1. 第 1 题:时间单位转换
- 2. 第 2 题:日期与时间运算
- 3. 第 3 题:查找指定日期的后一个周日的日期
- 4. 第 4 题:计算指定月份的日期范围
- 5. 第 5 题:将字符串转换为日期
- 6. 第 6 题:与时区相关的时间问题
- 试题代码地址
0.前言 时间和日期处理,是许多应用程序中绕不开的问题,例如日期与时间的转换、判断给定日期是周期等等问题,为了方便的处理上述问题,
Python
中内建的许多标准库(例如 time
、datetime
等)进行相关处理,接下来我们就通过 5 道编程题来掌握这些处理时间和日期的方法吧!1. 第 1 题:时间单位转换 知识点描述:时间转换,比如天到秒,小时到分钟等的转换。
问题描述:请从以下选项中选出能够将 3 天 6 小时 30 分 转化为秒的程序:
A.
from datetime import timedelta
days = 3
hours = 6
minutes = 30
result = timedelta(days=days, hours=hours, minutes=minutes).seconds
print(result)
B.
from datetime import timedelta
days = 3
hours = 6
minutes = 30
result = timedelta(days=days, hours=hours, minutes=minutes).total_seconds
print(result)
C.
from datetime import timedelta
days = 3
hours = 6
minutes = 30
result = timedelta(days=days, hours=hours, minutes=minutes).microseconds
print(result)
D.
from datetime import timedelta
days = 3
hours = 6
minutes = 30
result = timedelta(days=days, hours=hours, minutes=minutes).microseconds / 1000
print(result)
正确答案: A
2. 第 2 题:日期与时间运算 知识点描述:日期和时间处理问题。
问题描述:请从以下选项中选出能够计算日期间相隔天数的答案:
A.
from datetime import datetime
day_1 = datetime(2018,1,1)
day_2 = datetime(2021,10,4)
result = (day_1 - day_2).days
print(result)
B.
from datetime import datetime
day_1 = datetime(2018,1,1)
day_2 = datetime(2021,10,4)
result = (day_2 - day_1).days
print(result)
C.
from datetime import datetime
day_1 = datetime(2018,1,1)
day_2 = datetime(2021,10,4)
result = day_1 - day_2
print(result)
D.
from datetime import datetime
day_1 = datetime(2018,1,1)
day_2 = datetime(2021,10,4)
result = day_2 - day_1
print(result)
正确答案: B
3. 第 3 题:查找指定日期的后一个周日的日期 知识点描述:查找指定日期下一次星期日期出现的日期,比如 2021 年 10 月 3 日 的下一个星期日。
问题描述:计算 2021 年 10 月 3 日后一个星期日的日期,请从以下选项中选出你认为正确的答案:
A.
from datetime import datetime, timedelta
date = datetime(2021, 10, 3)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
def get_day(dayname, date):
day_num = date.weekday()
day_num_target = weekdays.index(dayname)
days_ago = (7 + day_num - day_num_target) % 7
target_date = date + timedelta(days_ago)
return target_date
result = get_day('Sunday', date)
print(result)
B.
from datetime import datetime, timedelta
date = datetime(2021, 10, 3)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
def get_day(dayname, date):
day_num = date.weekday()
day_num_target = weekdays.index(dayname)
days_ago = (7 + day_num - day_num_target) / 7
if days_ago == 0:
days_ago = 7
target_date = date + timedelta(days_ago)
return target_date
result = get_day('Sunday', date)
print(result)
C.
from datetime import datetime, timedelta
date = datetime(2021, 10, 3)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
def get_day(dayname, date):
day_num = date.weekday()
day_num_target = weekdays.index(dayname)
days_ago = (7 + day_num - day_num_target) // 7
if days_ago == 0:
days_ago = 7
target_date = date + timedelta(days_ago)
return target_date
result = get_day('Sunday', date)
print(result)
D.
from datetime import datetime, timedelta
date = datetime(2021, 10, 3)
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
def get_day(dayname, date):
day_num = date.weekday()
day_num_target = weekdays.index(dayname)
days_ago = (7 + day_num - day_num_target) % 7
if days_ago == 0:
days_ago = 7
target_date = date + timedelta(days_ago)
return target_date
result = get_day('Sunday', date)
print(result)
正确答案: D
4. 第 4 题:计算指定月份的日期范围 知识点描述:计算指定月份的日期范围,打印指定月份中所有日期。
问题描述:请从以下选项中选出可以打印出 2021年 10 月份所有日期(日期格式示例如下:2021-10-01)的答案:
A.
from datetime import datetime, date, timedelta
import calendar
def print_month_range(year, month):
start_date = datetime(year, month, 1)
_, days_in_date = calendar.monthrange(start_date.year, start_date.month)
for i in range(days_in_date):
print(start_date)
start_date += timedelta(days=i)
print_month_range(2021, 10)
B.
from datetime import datetime, date, timedelta
import calendar
def print_month_range(year, month):
start_date = datetime(year, month, 1)
_, days_in_date = calendar.monthrange(start_date.year, start_date.month)
for i in range(days_in_date):
next_date = start_date + timedelta(days=i)
print(next_date)
print_month_range(2021, 10)
C.
from datetime import datetime, date, timedelta
import calendar
def print_month_range(year, month):
start_date = datetime(year, month, 1)
_, days_in_date = calendar.monthrange(start_date.year, start_date.month)
for i in range(days_in_date):
next_date = start_date + timedelta(days=i)
print(next_date.date())
print_month_range(2021, 10)
D.
from datetime import datetime, date, timedelta
import calendar
def print_month_range(year, month):
start_date = datetime(year, month, 1)
_, days_in_date = calendar.monthrange(start_date.year, start_date.month)
for i in range(days_in_date+1):
next_date = start_date + timedelta(days=i)
print(next_date.date())
print_month_range(2021, 10)
正确答案: C
5. 第 5 题:将字符串转换为日期 知识点描述:将字符串转变为 datetime 对象,以便进行日期操作。
问题描述:请从以下选项中选出能够正确将字符串 “2021-10-03” 转变为 datetime 对象的程序:
A.
from datetime import datetime
def string_to_datetime(string):
date_obj = datetime.strptime(string, '%Y%m%d')
return date_obj
string = '2021-10-03'
result = string_to_datetime(string)
print(result)
B.
from datetime import datetime
def string_to_datetime(string):
date_obj = datetime.strptime(string, '%Y %m %d')
return date_obj
string = '2021-10-03'
result = string_to_datetime(string)
print(result)
C.
from datetime import datetime
def string_to_datetime(string):
date_obj = datetime.strptime(string, '%Y-%M-%D')
return date_obj
string = '2021-10-03'
result = string_to_datetime(string)
print(result)
D.
from datetime import datetime
def string_to_datetime(string):
date_obj = datetime.strptime(string, '%Y-%m-%d')
return date_obj
string = '2021-10-03'
result = string_to_datetime(string)
print(result)
【Python|仅需6道题轻松掌握Python时间和日期处理 | Python技能树征题】正确答案: D
6. 第 6 题:与时区相关的时间问题 知识点描述:时区间的相互转换。
问题描述:2019 年 4 月 29 日上午 9 时,2019 北京世界园艺博览会开园,请问以下哪个程序可以正确计算博览会开园时芝加哥时间:
A.
from datetime import datetime
from pytz import timezone
beijing = datetime(2019, 4, 29, 9, 0)
central = timezone('Asia/Shanghai')
loc_date = central.localize(beijing)
target_date = loc_date.astimezone(timezone('US/Central'))
print(target_date)
B.
from datetime import datetime
from pytz import timezone
beijing = datetime(2019, 4, 29, 9, 0)
central = timezone('Asia/Shanghai')
loc_date = central.localize(beijing)
target_date = timezone('US/Central')
print(target_date)
C.
from datetime import datetime
from pytz import timezone
beijing = datetime(2019, 4, 29, 9, 0)
central = timezone('Asia/Shanghai')
loc_date = central.localize(beijing)
target_date = central + loc_date.astimezone(timezone('US/Central'))
print(target_date)
D.
from datetime import datetime
from pytz import timezone
beijing = datetime(2019, 4, 29, 9, 0)
central = timezone('Asia/Shanghai')
loc_date = central.localize(beijing)
target_date = loc_date.astimezone(central)
print(target_date)
正确答案:A
试题代码地址 https://codechina.csdn.net/LOVEmy134611/python_problem
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)
- Python|Python 9.20