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 相关文章推荐
分享15个最受欢迎的Python开源框架
Jul 13 Python
Python中Django框架利用url来控制登录的方法
Jul 25 Python
Python selenium 三种等待方式解读
Sep 15 Python
python中logging模块的一些简单用法的使用
Feb 22 Python
TensorFlow基于MNIST数据集实现车牌识别(初步演示版)
Aug 05 Python
pandas中遍历dataframe的每一个元素的实现
Oct 23 Python
python实现名片管理器的示例代码
Dec 17 Python
python实现opencv+scoket网络实时图传
Mar 20 Python
解决python便携版无法直接运行py文件的问题
Sep 01 Python
5分钟快速掌握Python定时任务框架的实现
Jan 26 Python
python实战之用emoji表情生成文字
May 08 Python
Python如何用re模块实现简易tokenizer
May 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
消息持续发送的完整例子
2006/10/09 PHP
浅析echo(),print(),print_r(),return之间的区别
2013/11/27 PHP
php中file_get_content 和curl以及fopen 效率分析
2014/09/19 PHP
PHP获取远程http或ftp文件的md5值的方法
2019/04/15 PHP
PHP unset函数原理及使用方法解析
2020/08/14 PHP
Javascript 文件夹选择框的两种解决方案
2009/07/01 Javascript
js解析与序列化json数据(三)json的解析探讨
2013/02/01 Javascript
jquery实现的一个导航滚动效果具体代码
2013/05/27 Javascript
使用Web Uploader实现多文件上传
2016/06/08 Javascript
详解JS-- 浮点数运算处理
2016/11/28 Javascript
详解VueJs前后端分离跨域问题
2017/05/24 Javascript
node 使用 async 控制并发的方法
2018/05/07 Javascript
Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法
2018/09/15 Javascript
vue中使用protobuf的过程记录
2018/10/26 Javascript
Angular 2使用路由自定义弹出组件toast操作示例
2019/05/10 Javascript
vue页面引入three.js实现3d动画场景操作
2020/08/10 Javascript
[07:49]2014DOTA2国际邀请赛 Newbee夺冠后采访xiao8坦言奖金会上交
2014/07/23 DOTA
Python 制作糗事百科爬虫实例
2016/09/22 Python
使用pandas对矢量化数据进行替换处理的方法
2018/04/11 Python
Python使用matplotlib实现基础绘图功能示例
2018/07/03 Python
python中数字是否为可变类型
2020/07/08 Python
python3实现名片管理系统(控制台版)
2020/11/29 Python
css3进行截取替代js的substring
2013/09/02 HTML / CSS
销售所有的狗狗产品:Dog.com
2016/10/13 全球购物
中国跨镜手机配件批发在线商店:TVC-Mall
2019/08/20 全球购物
荷兰DOD药房中文官网:DeOnlineDrogist
2020/12/27 全球购物
销售自我评价
2013/10/22 职场文书
公司会计岗位职责
2014/02/13 职场文书
机械设计专业大学生职业生涯规划书范文
2014/09/13 职场文书
个人授权委托书范本格式
2014/10/12 职场文书
寒山寺导游词
2015/02/03 职场文书
英语导游词
2015/02/13 职场文书
运动会开幕式通讯稿
2015/07/18 职场文书
食品安全主题班会
2015/08/13 职场文书
2016学校先进党组织事迹材料
2016/02/29 职场文书
利用Python判断整数是否是回文数的3种方法总结
2021/07/07 Python