Python计算指定日期是今年的第几天(三种方法)
在Python编程中,有时我们需要计算一个指定日期是当年的第几天。本文提供了三种不同的方法来实现这个功能。这些方法对于日常编程或者面试中遇到类似问题时都非常实用。 我们来看第一种方法,通过字符串的`split()`方法来解析日期。例如,给定的日期字符串"2020-3-26"可以通过`split('-')`将其拆分为年、月、日三个部分。之后,将得到的字符串转换为整数,以便进行后续计算。以下是一个示例函数: ```python def cal_date_str_spilt(date): _year = int(date.split('-')[0]) _month = int(date.split('-')[1]) _day = int(date.split('-')[2]) return [_year, _month, _day] ``` 第二种方法使用Python的`time`库,通过`strptime()`函数解析日期字符串。`strptime()`函数接受一个日期字符串和一个格式化字符串,返回一个`struct_time`对象。我们可以从这个对象中提取出年、月、日。示例如下: ```python def cal_date_str_time(date): _date = time.strptime(date, '%Y-%m-%d') _year = _date.tm_year _month = _date.tm_mon _day = _date.tm_mday return [_year, _month, _day] ``` 接下来,我们需要判断给定的年份是否是闰年。闰年的规则是:能被400整除的年份,或者能被4整除但不能被100整除的年份。函数`judge_leap_year()`用于实现这个判断: ```python def judge_leap_year(year, month): if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) and month > 2: return 1 else: return 0 ``` 有了这些辅助函数,我们就可以计算指定日期是当年的第几天了。这可以通过累加每个月的天数来实现,同时考虑闰年2月的29天。`month_day`和`month_day_lep`两个列表分别表示非闰年和闰年的每月天数。`main()`函数中,我们调用这些辅助函数并进行计算: ```python def main(): date = input("请输入日期,以'-'分隔:") # 方法一和方法二的计算 date_list_1 = cal_date_str_spilt(date) date_list_2 = cal_date_str_time(date) sum_1, sum_2 = 0, 0 month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] month_day_lep = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] sum_1 += sum(month_day[:date_list_1[1] - 1]) + date_list_1[2] + judge_leap_year(date_list_1[0], date_list_1[1]) sum_2 += sum(month_day[:date_list_2[1] - 1]) + date_list_2[2] + judge_leap_year(date_list_2[0], date_list_2[1]) # 方法三使用datetime库 _sum = datetime.date(date_list_1[0], date_list_1[1], date_list_1[2]) sum_3 = _sum.strftime('%j') # 输出结果 print('今天是今年的第' + str(sum_1) + '天') print('今天是今年的第' + str(sum_2) + '天') if sum_3[0] == '0' and sum_3[1] == '0': print('今天是今年的第' + str(sum_3[-1:]) + '天') elif sum_3[0] == '0': print('今天是今年的第' + str(sum_3[-2:]) + '天') else: print('今天是今年的第' + str(sum_3) + '天') if __name__ == '__main__': main() ``` 第三种方法使用了Python的`datetime`库。`datetime.date()`函数创建了一个日期对象,然后使用`strftime('%j')`格式化输出,它会返回一个三位数表示当年的天数,前导零会被忽略。这种方法更为简洁和直接。 以上三种方法都可以有效地计算出指定日期是当年的第几天。在实际应用中,根据项目需求和个人偏好,可以选择最适合的方法。同时,这段代码也可以作为一个很好的学习材料,帮助理解日期处理和闰年判断的相关知识。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/release/download_crawler_static/12853914/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 7
- 资源: 964
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)