个人主页: Aileen_0v0 热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​个人格言:“没有罗马,那就自己创造罗马~”

#mermaid-svg-MlNnGPJxLgUD4Z4h {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .error-icon{fill:#552222;}#mermaid-svg-MlNnGPJxLgUD4Z4h .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MlNnGPJxLgUD4Z4h .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .marker.cross{stroke:#333333;}#mermaid-svg-MlNnGPJxLgUD4Z4h svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .cluster-label text{fill:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .cluster-label span{color:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .label text,#mermaid-svg-MlNnGPJxLgUD4Z4h span{fill:#333;color:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .node rect,#mermaid-svg-MlNnGPJxLgUD4Z4h .node circle,#mermaid-svg-MlNnGPJxLgUD4Z4h .node ellipse,#mermaid-svg-MlNnGPJxLgUD4Z4h .node polygon,#mermaid-svg-MlNnGPJxLgUD4Z4h .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .node .label{text-align:center;}#mermaid-svg-MlNnGPJxLgUD4Z4h .node.clickable{cursor:pointer;}#mermaid-svg-MlNnGPJxLgUD4Z4h .arrowheadPath{fill:#333333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-MlNnGPJxLgUD4Z4h .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-MlNnGPJxLgUD4Z4h .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MlNnGPJxLgUD4Z4h .cluster text{fill:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h .cluster span{color:#333;}#mermaid-svg-MlNnGPJxLgUD4Z4h 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-MlNnGPJxLgUD4Z4h :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

Always seek out the seed of triumph inevery adversity.

文章目录

应用一应用二

#mermaid-svg-zGLqSFRpGlvyy4qs {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .error-icon{fill:#552222;}#mermaid-svg-zGLqSFRpGlvyy4qs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zGLqSFRpGlvyy4qs .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-zGLqSFRpGlvyy4qs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zGLqSFRpGlvyy4qs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zGLqSFRpGlvyy4qs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zGLqSFRpGlvyy4qs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zGLqSFRpGlvyy4qs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zGLqSFRpGlvyy4qs .marker.cross{stroke:#333333;}#mermaid-svg-zGLqSFRpGlvyy4qs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zGLqSFRpGlvyy4qs .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .cluster-label text{fill:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .cluster-label span{color:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .label text,#mermaid-svg-zGLqSFRpGlvyy4qs span{fill:#333;color:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .node rect,#mermaid-svg-zGLqSFRpGlvyy4qs .node circle,#mermaid-svg-zGLqSFRpGlvyy4qs .node ellipse,#mermaid-svg-zGLqSFRpGlvyy4qs .node polygon,#mermaid-svg-zGLqSFRpGlvyy4qs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zGLqSFRpGlvyy4qs .node .label{text-align:center;}#mermaid-svg-zGLqSFRpGlvyy4qs .node.clickable{cursor:pointer;}#mermaid-svg-zGLqSFRpGlvyy4qs .arrowheadPath{fill:#333333;}#mermaid-svg-zGLqSFRpGlvyy4qs .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zGLqSFRpGlvyy4qs .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zGLqSFRpGlvyy4qs .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-zGLqSFRpGlvyy4qs .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-zGLqSFRpGlvyy4qs .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zGLqSFRpGlvyy4qs .cluster text{fill:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs .cluster span{color:#333;}#mermaid-svg-zGLqSFRpGlvyy4qs 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-zGLqSFRpGlvyy4qs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

Python标准库

友情链接,点击带颜色字体即可跳转 Python标准库链接

Python标准库datetime模块包含用于日期(date)和时间(time)的数据类型。 Python的datetime模块提供了处理日期和时间的功能。它包含了多个类和函数,可以用来创建、操作、格式化和计算日期和时间。

datetime模块 下面是一些datetime模块的常用类和函数以及它们的使用场景:

1.datetime类:用来表示日期和时间的类。可以创建一个datetime对象,然后对其进行各种操作,比如获取日期、时间、年、月、日等。

import datetime

# 创建一个datetime对象

now = datetime.datetime.now()

# 获取日期和时间

print(now)

# 获取年、月、日、时、分、秒等

print(now.year)

print(now.month)

print(now.day)

print(now.hour)

print(now.minute)

print(now.second)

2.timedelta类:用来表示时间间隔的类。可以进行日期和时间的加减操作,计算两个日期之间的时间间隔等。

import datetime

# 创建一个timedelta对象,表示两天的时间间隔

delta = datetime.timedelta(days=2)

# 获取当前日期

now = datetime.datetime.now()

# 计算两天后的日期

future_date = now + delta

print(future_date)

3. strftime()函数:用来将日期和时间格式化为字符串。可以使用各种格式化指令来指定输出的格式。

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

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: