使用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库更加熟悉,未来可以拓展更多的方法来输入日期格式。

相关推荐
apocelipes6 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780517 小时前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent12 小时前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m62515 小时前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
SelectDB1 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码2 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵2 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li2 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸2 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学2 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员