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实现通过pil模块对图片格式进行转换的方法
Mar 24 Python
Python实现在线程里运行scrapy的方法
Apr 07 Python
python字典基本操作实例分析
Jul 11 Python
Python如何通过subprocess调用adb命令详解
Aug 27 Python
对Pandas DataFrame缺失值的查找与填充示例讲解
Nov 06 Python
对python 自定义协议的方法详解
Feb 13 Python
python字典改变value值方法总结
Jun 21 Python
简单了解django索引的相关知识
Jul 17 Python
python打造爬虫代理池过程解析
Aug 15 Python
python正则表达式匹配IP代码实例
Dec 28 Python
python 非线性规划方式(scipy.optimize.minimize)
Feb 11 Python
Python 数据可视化之Bokeh详解
Nov 02 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
php实现通用的信用卡验证类
2015/03/24 PHP
PHP文件读取功能的应用实例
2015/05/08 PHP
php中关于socket的系列函数总结
2015/05/18 PHP
PHP对称加密函数实现数据的加密解密
2016/10/27 PHP
ThinkPHP删除栏目(实现批量删除栏目)
2017/06/21 PHP
JSON 数据格式介绍
2012/01/13 Javascript
设为首页和收藏的Javascript代码(亲测兼容IE,Firefox,chrome等浏览器)
2013/11/18 Javascript
js调用iframe实现打印页面内容的方法
2014/03/04 Javascript
第一次动手实现bootstrap table分页效果
2016/09/22 Javascript
用angular实现多选按钮的全选与反选实例代码
2017/05/23 Javascript
jQuery EasyUI Layout实现tabs标签的实例
2017/09/26 jQuery
Vue修改mint-ui默认样式的方法
2018/02/03 Javascript
layui radio点击事件实现input显示和隐藏的例子
2019/09/02 Javascript
JavaScript canvas仿代码流瀑布
2020/02/10 Javascript
详解Nuxt内导航栏的两种实现方式
2020/04/16 Javascript
手把手教你如何编译打包video.js
2020/12/09 Javascript
JS创建自定义对象的六种方法总结
2020/12/15 Javascript
[02:14]DOTA2英雄基础教程 修补匠
2013/12/23 DOTA
[02:33]2018 DOTA2亚洲邀请赛回顾视频 再次拾起那些美妙的时刻
2018/04/10 DOTA
Python 第一步 hello world
2009/09/25 Python
在Python中使用dict和set方法的教程
2015/04/27 Python
Python生成密码库功能示例
2017/05/23 Python
django之session与分页(实例讲解)
2017/11/13 Python
快速了解Python相对导入
2018/01/12 Python
python对Excel的读取的示例代码
2020/02/14 Python
CSS3中各种颜色属性的使用教程
2016/05/17 HTML / CSS
突袭HTML5之Javascript API扩展4—拖拽(Drag/Drop)概述
2013/01/31 HTML / CSS
倩碧香港官方网站:Clinique香港
2017/11/13 全球购物
UNOde50美国官网:西班牙珠宝品牌
2020/08/15 全球购物
"引用"与指针的区别是什么
2016/09/07 面试题
linux面试题参考答案(9)
2015/01/07 面试题
家长评语和期望
2014/02/10 职场文书
民主生活会整改措施(党员)
2014/09/18 职场文书
2015年行政人事部工作总结
2015/05/13 职场文书
Python探索生命起源 matplotlib细胞自动机动画演示
2022/04/21 Python
Golang gRPC HTTP协议转换示例
2022/06/16 Golang