Python编程实现输入某年某月某日计算出这一天是该年第几天的方法


Posted in Python onApril 18, 2017

本文实例讲述了Python编程实现输入某年某月某日计算出这一天是该年第几天的方法。分享给大家供大家参考,具体如下:

#基于 Python3

一种做法:

def is_leap_year(year): # 判断闰年,是则返回True,否则返回False
  if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    return True
  else:
    return False
def function1(year, month, day): # 计算给定日期是那一年的第几天
  leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if is_leap_year(year):
    result = sum(leap_year[:month - 1]) + day
  else:
    result = sum(no_leap_year[:month - 1]) + day
  return result

但是如果是你自己遇到了这样的需求,那么就没必要这么复杂了。因为Python内置了完善的时间和日期处理函数。

import datetime
import time
def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果
  date = datetime.date(year, month, day)
  return date.strftime('%j')

需要注意的是,上面的写法里函数的参数分别是年月日的整数,如果你想传入字符串,比如"2016-10-1",那就需要先对字符串做处理了。

同样的,也可以自己做或者用内置函数。

# 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理
_input = '2016-10-1'
_year1 = int(_input.split('-')[0])
_month1 = int(_input.split('-')[1])
_day1 = int(_input.split('-')[2])
# 当然你也可以用datetime的内置方法进行格式处理
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday

下面是完整的代码,测试"2016-10-1"的结果均为275。

import datetime
import time
def is_leap_year(year): # 判断闰年,是则返回True,否则返回False
  if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    return True
  else:
    return False
def function1(year, month, day): # 计算给定日期是那一年的第几天
  leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if is_leap_year(year):
    result = sum(leap_year[:month - 1]) + day
  else:
    result = sum(no_leap_year[:month - 1]) + day
  return result
def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果
  date = datetime.date(year, month, day)
  return date.strftime('%j')
print(function1(2016, 10, 1))
print(function2(2016, 10, 1))
# 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理
_input = '2016-10-1'
_split = _input.split('-')
_year1 = int(_split[0])
_month1 = int(_split[1])
_day1 = int(_split[2])
print(function1(_year1, _month1, _day1))
print(function2(_year1, _month1, _day1))
# 当然你也可以用datetime的内置方法进行格式处理
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday
print(function1(_year2, _month2, _day2))
print(function2(_year2, _month2, _day2))
# 后面发现我为了编函数写复杂了,如果输入是字符串其实一句话就好
import time
_input = '2016-10-1'
# 详见Python日期和字符串格式互相转换 https://3water.com/article/66019.htm
t = time.strptime(_input, '%Y-%m-%d')
print(time.strftime('%j',t))

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.3water.com/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.3water.com/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.3water.com/bianmin/yinli2yangli

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python将阿拉伯数字转换为罗马数字的方法
Jul 10 Python
Python基于select实现的socket服务器
Apr 13 Python
详解Python if-elif-else知识点
Jun 11 Python
Python爬虫的两套解析方法和四种爬虫实现过程
Jul 20 Python
Python爬取qq空间说说的实例代码
Aug 17 Python
详解python中__name__的意义以及作用
Aug 07 Python
Python代理IP爬虫的新手使用教程
Sep 05 Python
余弦相似性计算及python代码实现过程解析
Sep 18 Python
Python文件夹批处理操作代码实例
Jul 21 Python
解决python打开https出现certificate verify failed的问题
Sep 03 Python
python调用jenkinsAPI构建jenkins,并传递参数的示例
Dec 09 Python
Python中json.dumps()函数的使用解析
May 17 Python
浅析python递归函数和河内塔问题
Apr 18 #Python
Python使用正则表达式实现文本替换的方法
Apr 18 #Python
Python外星人入侵游戏编程完整版
Mar 30 #Python
Python随机数用法实例详解【基于random模块】
Apr 18 #Python
django使用图片延时加载引起后台404错误
Apr 18 #Python
使用Python3制作TCP端口扫描器
Apr 17 #Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
Apr 17 #Python
You might like
解析htaccess伪静态的规则
2013/06/18 PHP
完美解决JS中汉字显示乱码问题(已解决)
2006/12/27 Javascript
JS限制Textarea文本域字符个数的具体实现
2013/08/02 Javascript
JS实现FLASH幻灯片图片切换效果的方法
2015/03/04 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
2016/01/26 Javascript
JS经典正则表达式笔试题汇总
2016/12/15 Javascript
JS自动生成动态HTML验证码页面
2017/06/14 Javascript
Vue学习笔记进阶篇之多元素及多组件过渡
2017/07/19 Javascript
ionic2屏幕适配实现适配手机、平板等设备的示例代码
2017/08/11 Javascript
详解Angular4 路由设置相关
2017/08/26 Javascript
测试、预发布后用python检测网页是否有日常链接
2014/06/03 Python
教你如何将 Sublime 3 打造成 Python/Django IDE开发利器
2014/07/04 Python
Python实用日期时间处理方法汇总
2015/05/09 Python
Python+django实现文件上传
2016/01/17 Python
python实现可以断点续传和并发的ftp程序
2016/09/13 Python
Python算法应用实战之栈详解
2017/02/04 Python
Python基于回溯法子集树模板解决0-1背包问题实例
2017/09/02 Python
Python FTP两个文件夹间的同步实例代码
2018/05/25 Python
python生成器与迭代器详解
2019/01/01 Python
安装好Pycharm后如何配置Python解释器简易教程
2019/06/28 Python
如何使用python把ppt转换成pdf
2019/06/29 Python
django admin.py 外键,反向查询的实例
2019/07/26 Python
python多进程并行代码实例
2019/09/30 Python
Django通过dwebsocket实现websocket的例子
2019/11/15 Python
Django serializer优化类视图的实现示例
2020/07/16 Python
运动鞋、足球鞋和慕尼黑球衣:Sport Münzinger
2019/08/26 全球购物
ruby如何进行集成操作?Ruby能进行多重继承吗?
2013/10/16 面试题
自我鉴定注意事项
2014/01/19 职场文书
社区学雷锋活动策划方案
2014/01/30 职场文书
介绍长城的导游词
2015/01/30 职场文书
大学生心理健康活动总结
2015/05/08 职场文书
就业意向书范本
2015/05/11 职场文书
2016年“11.11”光棍节活动总结
2016/04/05 职场文书
python中pandas.read_csv()函数的深入讲解
2021/03/29 Python
Python手拉手教你爬取贝壳房源数据的实战教程
2021/05/21 Python
CSS filter 有什么神奇用途
2021/05/25 HTML / CSS