当前位置: 首页 > news >正文

python -日期与天数的转换

日期与天数的转换

  • 闰年判断
  • 每个月的对应的天数

闰年判断

  • 一个年份是闰年当且仅当它满足下列两种情况其中的一种:
    • 这个年份是4 的整数倍,但不是100 的整数倍;
    • 这个年份是 400 的整数倍。

每个月的对应的天数

  • 每年12 个月份,其中1,3,5,7,8,10,12,每个月有 31 天;
  • 4,6,9,11月每个月有 30 天;
  • 对于 2 月,闰年有 29 天,平年有 28 天。
import sys# 日期处理-日期转换为天数,根据某年过去的天数获取具体的日期
class DateHandle:def __init__(self) -> None:self.mp = {1:31, 3:31, 5:31, 7:31, 8:31, 10:31, 12:31, 4:30, 6:30, 9:30, 11:30, 2:28}  # 闰年判断def check(self, year):if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):return Truereturn False# 获取月份对应的天数def init(self, year):if self.check(year): # 瑞年self.mp[2] = 29   # 根据年月日获取对应的天数def getDays(self, year, month, day):cnt = 0for m in range(1, month):cnt += self.mp[m]cnt += dayreturn cnt# 根据某年的天数获取年月日def getDate(self, year, days):cnt, month = 0, 1for i in range(1, 13):month = iif cnt + self.mp[i] <= days:cnt += self.mp[i]else:break    date = days - cnt return "%d %d %d" % (year, month, date)  year, month, day = 2025, 6, 6
d = DateHandle()
d.init(year)
cnt  = d.getDays(year, month, day)
date = d.getDate(year, cnt)
print('year: %d, month: %d, day: %d , days is %d' % (year, month, day, cnt))  
print('year is %d, days is %d, conver to date is %s' % (year, cnt, date))   ##------------------------output--------------------##
#	year: 2025, month: 6, day: 6 , days is 157
# 	year is 2025, days is 157, conver to date is 2025 6 6
##------------------------over----------------------##
http://www.lqws.cn/news/558235.html

相关文章:

  • autoas/as 工程的RTE静态消息总线实现与端口数据交换机制详解
  • 解决flash-attn安装报错的问题
  • 【C】陷波滤波器
  • 鸿蒙开发:资讯项目实战之底部导航封装
  • MySQL之MVCC实现原理深度解析
  • 类和对象(中)
  • springboot+Vue驾校管理系统
  • 开疆智能ModbusTCP转CClinkIE网关连接台达DVP-ES3 PLC配置案例
  • Java-正则表达式
  • 测量 Linux 中进程上下文切换需要的时间
  • cocos creator 3.8 - 精品源码 - 挪车超人(挪车消消乐)
  • 同步日志系统深度解析【链式调用】【宏定义】【固定缓冲区】【线程局部存储】【RAII】
  • 蚂蚁百宝箱体验:如何快速创建“旅游小助手”AI智能体
  • LINUX628 NFS 多web;主从dns;ntp;samba
  • AlphaGenome:基因组学领域的人工智能革命
  • Linux离线搭建Redis (centos7)详细操作步骤
  • 深入解析 Electron 核心模块:构建跨平台桌面应用的关键
  • 《Go语言高级编程》玩转RPC
  • Vue.js 中的 v-model 和 :value:理解父子组件的数据绑定
  • 网络 : 传输层【UDP协议】
  • (线性代数)矩阵的奇异值Singular Value
  • WPS之PPT镂空效果实现
  • 笔记07:网表的输出与导入
  • spring中maven缺少包如何重新加载,报错java: 程序包org.springframework.web.reactive.function不存在
  • FPGA产品
  • 深入理解Java四大引用:强引用、软引用、弱引用与虚引用
  • 2.2.3、CAN总线-位时间特性、中断
  • 开源项目推荐:MCP Registry——管理MCP服务器的利器
  • git 变基:git rebase
  • 使用cmake+vs2022编译win环境下grpc(不建议拉取最新版本grpc(注意本文时间是2025/6/28))