【备战蓝桥杯】如何使用Python 内置模块datetime去计算我与CSDN相遇的天数_蓝桥杯可以用datetime包吗(1)

import datetime

# 获取当前日期和时间
now = datetime.datetime.now()

# 将日期和时间格式化为字符串
formatted = now.strftime("%Y-%m-%d %H:%M:%S")

print(formatted)

在这里插入图片描述

  • 4. strptime()函数:用来将字符串解析为datetime对象。
  • 可以使用与strftime()函数相同的格式化指令来解析字符串。
import datetime

# 将字符串解析为datetime对象
date_str = "2022-01-01"
date = datetime.datetime.strptime(date_str, "%Y-%m-%d")

print(date)

在这里插入图片描述

datetime模块的使用场景包括但不限于以下几个方面:

  • 处理日期和时间的计算:可以通过datetime类和timedelta类来进行日期和时间的计算,比如计算两个日期之间的时间间隔、计算某个日期的前后几天等。
  • 时间格式化:可以使用strftime()函数将日期和时间格式化为特定的字符串格式,比如将日期和时间格式化为"yyyy-mm-dd"或"hh:mm:ss"等。
  • 字符串解析为日期和时间:可以使用strptime()函数将字符串解析为datetime对象,便于进行日期和时间的处理和计算。

#mermaid-svg-1muiWhRtJ41eSxkL {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .error-icon{fill:#552222;}#mermaid-svg-1muiWhRtJ41eSxkL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1muiWhRtJ41eSxkL .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-1muiWhRtJ41eSxkL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1muiWhRtJ41eSxkL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1muiWhRtJ41eSxkL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1muiWhRtJ41eSxkL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1muiWhRtJ41eSxkL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1muiWhRtJ41eSxkL .marker.cross{stroke:#333333;}#mermaid-svg-1muiWhRtJ41eSxkL svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1muiWhRtJ41eSxkL .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .cluster-label text{fill:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .cluster-label span{color:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .label text,#mermaid-svg-1muiWhRtJ41eSxkL span{fill:#333;color:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .node rect,#mermaid-svg-1muiWhRtJ41eSxkL .node circle,#mermaid-svg-1muiWhRtJ41eSxkL .node ellipse,#mermaid-svg-1muiWhRtJ41eSxkL .node polygon,#mermaid-svg-1muiWhRtJ41eSxkL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1muiWhRtJ41eSxkL .node .label{text-align:center;}#mermaid-svg-1muiWhRtJ41eSxkL .node.clickable{cursor:pointer;}#mermaid-svg-1muiWhRtJ41eSxkL .arrowheadPath{fill:#333333;}#mermaid-svg-1muiWhRtJ41eSxkL .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1muiWhRtJ41eSxkL .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1muiWhRtJ41eSxkL .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-1muiWhRtJ41eSxkL .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-1muiWhRtJ41eSxkL .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1muiWhRtJ41eSxkL .cluster text{fill:#333;}#mermaid-svg-1muiWhRtJ41eSxkL .cluster span{color:#333;}#mermaid-svg-1muiWhRtJ41eSxkL div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-1muiWhRtJ41eSxkL :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;}

datetime的应用

应用一

根据加入时间计算出自己加入CSDN的天数

在这里插入图片描述

#mermaid-svg-6deX7Dis08zvmtPl {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-6deX7Dis08zvmtPl .error-icon{fill:#552222;}#mermaid-svg-6deX7Dis08zvmtPl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6deX7Dis08zvmtPl .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-6deX7Dis08zvmtPl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6deX7Dis08zvmtPl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6deX7Dis08zvmtPl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6deX7Dis08zvmtPl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6deX7Dis08zvmtPl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6deX7Dis08zvmtPl .marker.cross{stroke:#333333;}#mermaid-svg-6deX7Dis08zvmtPl svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6deX7Dis08zvmtPl .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-6deX7Dis08zvmtPl .cluster-label text{fill:#333;}#mermaid-svg-6deX7Dis08zvmtPl .cluster-label span{color:#333;}#mermaid-svg-6deX7Dis08zvmtPl .label text,#mermaid-svg-6deX7Dis08zvmtPl span{fill:#333;color:#333;}#mermaid-svg-6deX7Dis08zvmtPl .node rect,#mermaid-svg-6deX7Dis08zvmtPl .node circle,#mermaid-svg-6deX7Dis08zvmtPl .node ellipse,#mermaid-svg-6deX7Dis08zvmtPl .node polygon,#mermaid-svg-6deX7Dis08zvmtPl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6deX7Dis08zvmtPl .node .label{text-align:center;}#mermaid-svg-6deX7Dis08zvmtPl .node.clickable{cursor:pointer;}#mermaid-svg-6deX7Dis08zvmtPl .arrowheadPath{fill:#333333;}#mermaid-svg-6deX7Dis08zvmtPl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6deX7Dis08zvmtPl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6deX7Dis08zvmtPl .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-6deX7Dis08zvmtPl .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-6deX7Dis08zvmtPl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6deX7Dis08zvmtPl .cluster text{fill:#333;}#mermaid-svg-6deX7Dis08zvmtPl .cluster span{color:#333;}#mermaid-svg-6deX7Dis08zvmtPl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6deX7Dis08zvmtPl :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;}#mermaid-svg-6deX7Dis08zvmtPl .someclass>*{fill:#f96!important;}#mermaid-svg-6deX7Dis08zvmtPl .someclass span{fill:#f96!important;}

解题步骤

1.根据日期构造出datetime类型的变量

2.把两个变量进行相减得到结果即为所求

#先构造 datetime 变量
from datetime import  datetime as ts
date1 = ts(year= 2024, month= 1, day= 16)
date2 = ts(year= 2023, month= 3, day= 14)
print(date1 - date2)

应用二

输入2023年的一个日期,输出星期几

写法1:

import datetime
date_str = input("输入日期(格式:YYYY-MM-DD):")
year,month,day = map(int,date_str.split("-"))
date1= datetime.date(year,month,day)
weekday =date1.strftime("%A")
print("这个日期是星期:",weekday)

写法2:

from datetime import date
date_str = input("输入日期(格式:YYYY-MM-DD):")
year,month,day = map(int,date_str.split("-"))
date2= datetime.date(year,month,day)
weekday =date2.strftime("%A")
print("这个日期是星期:",weekday)

在这里插入图片描述

从上面的代码我们可以看出datetime这个大模块里面包含了日期【date】和时间【time】模块,在这里我们就列举了常用的,如果小伙伴对它的其它模块用法感兴趣可以点击👉Python官方文件夹进行深入学习。
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:
https://cloud.tencent.com/developer/support-plan?invite_code=d3ubed05qudm

](https://img-blog.csdnimg.cn/0ee6c4ec414740b0a0404c5161cdadc7.gif#pic_center)

](https://img-blog.csdnimg.cn/cc002cbd5c414c5393e19c5e0a0dbf20.gif#pic_center#pic_center)

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值