一篇文章带你了解python标准库--datetime模块
作者:wakeyo_J 时间:2022-08-21 03:42:04
1. datetime模块介绍
1.1 datetime模块包含的类
1.2 datetime模块中包含的常量
2. datetime实例的方法
案例代码
import locale
from datetime import datetime,date,time
locale.setlocale(locale.LC_CTYPE, 'chinese')
print(datetime.now()) #返回当天的日期和时间,datetime类型
today=datetime.now() #定义today为当前日期时间对象
print(datetime.date(today)) #返回当天的日期对象,date类型
print(datetime.time(today)) #返回当天的时间对象,time类型
print(datetime.ctime(today)) #获取“星期,月,日,时,分,秒,年”格式的字符串
print(datetime.utcnow()) #返回当前的UTC日期和时间,datetime类型
print(datetime.timestamp(today)) #返回当天的时间戳(UNIX时间戳),浮点数类型
print(datetime.fromtimestamp(datetime.timestamp(today))) #根据时间戳返回UTC日期时间,datetime类型
date1=date(2018,2,12)
time1=time(20,53,48)
print(datetime.combine(date1,time1)) #绑定日期、时间,生成新的datetime对象
newDatetime=datetime.strptime('2021-08-03 22:53:0', '%Y-%m-%d %H:%M:%S') #使用字符串和指定格式生成新的datetime对象
print(newDatetime)
for tv in datetime.timetuple(today):
print(tv)
print(today.isocalendar()) #ISO格式的日期
print(today.strftime("%Y年%m月%d日 %H:%M:%S %p"))
结果
3. 日期格式化符号
strftime()方法和strptime()方法的时间日期格式化符号及所代表意思
来源:https://blog.csdn.net/m0_55697123/article/details/119360256
标签:python,datetime模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
超详细的sql2005图解安装全过程第1/2页
2024-01-24 17:11:55
![](https://img.aspxhome.com/file/2023/2/100002_0s.jpg)
如何用表单在线建立目录?
2010-06-16 09:49:00
Golang实现字符串倒序的几种解决方案
2024-01-30 20:01:37
详细介绍pandas的DataFrame的append方法使用
2022-08-25 07:00:34
![](https://img.aspxhome.com/file/2023/1/93601_0s.png)
SQL Server 定时访问url激活数据同步示例
2024-01-24 11:22:50
你凭什么说你的网站用户体验好
2011-03-31 17:08:00
教你漂亮打印Pandas DataFrames和Series
2022-11-10 19:08:56
![](https://img.aspxhome.com/file/2023/8/65848_0s.png)
Python学习之循环方法详解
2021-04-13 21:07:15
![](https://img.aspxhome.com/file/2023/3/131643_0s.png)
SQL SERVER 2012新增函数之逻辑函数IIF
2024-01-16 05:39:46
详解Python中的Cookie模块使用
2023-01-02 21:30:39
![](https://img.aspxhome.com/file/2023/0/107290_0s.jpg)
python实现桌面气泡提示功能
2023-07-13 06:37:13
![](https://img.aspxhome.com/file/2023/0/94070_0s.jpg)
Python代码列表求并集,交集,差集
2021-12-03 03:33:38
![](https://img.aspxhome.com/file/2023/7/98957_0s.png)
一次性彻底讲透Python中pd.concat与pd.merge
2023-02-06 19:04:33
![](https://img.aspxhome.com/file/2023/7/81497_0s.png)
Pycharm之快速定位到某行快捷键的方法
2022-06-20 14:07:53
![](https://img.aspxhome.com/file/2023/7/121417_0s.jpg)
python 教程实现 turtle海龟绘图
2022-03-19 10:45:35
![](https://img.aspxhome.com/file/2023/4/68834_0s.png)
Python 实现日志同时输出到屏幕和文件
2021-06-25 23:04:29
Sql Server 数据库超时问题的解决方法
2009-01-13 14:11:00
PHP attributes()函数讲解
2023-06-04 09:33:02
![](https://img.aspxhome.com/file/2023/0/55410_0s.png)
JS 中如何判断 null
2007-12-13 20:28:00
MySQL分区之KEY分区详解
2024-01-26 16:24:54
![](https://img.aspxhome.com/file/2023/6/68596_0s.png)