Python 3.X 练习集100题 04
输入某年某月某日,判断这一天是这一年的第几天?
方法1:
import time test_time = input("请输入日期(年-月-日):") time_struct = time.strptime(test_time, "%Y-%m-%d") time_year = time_struct.tm_year time_yday = time_struct.tm_yday print("{}是{}年这一年中的第{}天".format(test_time, time_year, time_yday))
方法2:
import datetime date_time = input("请输入日期(年-月-日):") datetime_struct = datetime.datetime.strptime(date_time, "%Y-%m-%d") time_year = datetime_struct.timetuple().tm_year time_yday = datetime_struct.timetuple().tm_yday print("{}是{}年这一年中的第{}天".format(date_time, time_year, time_yday))
方法3:
def get_daynum(): year = input("请输入年份:") month = input("请输入月份:") day = input("请输入天:") date1 = datetime.date(year=int(year), month=int(month), day=int(day)) date2 = datetime.date(year=int(year), month=1, day=1) return ((date1 - date2).days + 1) if __name__ == '__main__': print(get_daynum())