使用detetime库输出10种不同日期格式

1 问题

如何使用python中的datetime库输入10种不同的日期格式,输出自己的生日日期?

2 方法

(1)、创建一个datetime的对象,获取时间。

(2)、dt.isocalendar对象返回一个元组,里面有三个元素,对应年、周、本周第几天,是ISO日历。

(3)、通过dt.year,dt.month,dt.day,dt.hour,dt.minute获取相应的年,月,日,天,时,分,通过配合format函数进行输出。

(4)、使用strftime()函数格式化去掉后面的微秒毫秒级别时间。

代码清单1

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| from datetime import datetime: Birthday=datetime(2003.8.29) a=Birthday.isocalendar() print('你的生日是:') print(Birthday.year,Birthday.month,Birthday.day) print(Birthday.strftime('%Y-%m-%d')) print(Birthday.strftime('%d,%B,%Y')) print(Birthday.strftime('%Y/%m/%d')) print('{0:%Y}年{0:%m}月{0:%d}日'.format(Birthday)) print(Birthday.strftime('%Y%m%d')) print(Birthday.strftime('%m%d%Y')) print('{}年{}周{}星期'format(0,1,2)) print(Birthday.strftime('%y-%m-%d-%A')) |

3 结语

针对如何输入10种不同的日期格式的问题,提出使用datetime库的方法,通过datetime实验,证明该方法是有效的,让我们对datetime库更加熟悉,未来可以拓展更多的方法来输入日期格式。

相关推荐
用户8356290780512 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon4 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly4 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程4 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly6 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风12 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风12 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风1 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风1 天前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python