如何使用python获取现在的日期与时间
作者:迎着黎明那道光 时间:2021-07-21 16:50:12
在python 中使用datatime库,可以用来获取现在日期和时间
一、datetime库简介
datetime有五个类:
datetime.date表示日期
datetime.datetime表示日期时间
datetime.time表示时间
datetime.timedelta表示时间间隔
datetime.tzinfo表示时区
二、datetime.datetime.now
datetime.datetime.now()
可以返回系统现在的时间
使用如下:
#pmj 2021-4-5 获取当前的日期和时间
import datetime
import time
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是 %s" %i.day)
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是 %s" %i.second)
三、运行效果
还有一种如果想要让所得到的日期符合ISO标准,可以通过isocalendar()来写,如:
x = datetime.date(2020,7,21)
x.isocalendar()
则会得到(2020,7,21)
或者是通过isoformat()来编写,返回一个ISO 8601标准的书写方式。如:
x = datetime.date(2020,7,21)
x.isoformat()
则会得到2020-7-21的结果显示。
来源:https://blog.csdn.net/pengmingjv/article/details/115447190
标签:python,日期时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python绘制圆柱体的方法
2022-07-31 02:00:31
![](https://img.aspxhome.com/file/2023/6/96946_0s.jpg)
修改SQL Server 2005 sa用户密码的方法
2008-12-10 14:41:00
基于python的BP神经网络及异或实现过程解析
2021-10-29 00:02:01
![](https://img.aspxhome.com/file/2023/8/72778_0s.jpg)
简单介绍Python中利用生成器实现的并发编程
2022-04-29 00:56:12
prototype.js常用函数及其用法
2007-11-21 20:47:00
关于python环境变量如何配置
2023-06-02 14:23:09
![](https://img.aspxhome.com/file/2023/7/98937_0s.jpg)
python实现的多线程端口扫描功能示例
2023-02-02 10:18:29
![](https://img.aspxhome.com/file/2023/3/118613_0s.png)
ASP程序直接连接MYSQL数据库
2008-10-03 12:13:00
关于 Web,你可能不知道的
2008-09-18 12:09:00
![](https://img.aspxhome.com/file/UploadPic/20089/18/2008918121258407s.jpg)
Python实现螺旋矩阵的填充算法示例
2022-06-30 00:18:47
![](https://img.aspxhome.com/file/2023/9/68429_0s.png)
css学习笔记:为什么在IE6下图片会往下掉一行
2009-06-19 12:50:00
![](https://img.aspxhome.com/file/UploadPic/20099/25/2009925225543206s.jpg)
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
2021-03-17 16:31:22
![](https://img.aspxhome.com/file/2023/6/120406_0s.jpg)
Python Pillow(PIL)库的用法详解
2022-01-31 13:43:26
![](https://img.aspxhome.com/file/2023/8/81458_0s.jpg)
解读ASP.NET 5 & MVC6系列教程(12):基于Lamda表达式的强类型Routing实现
2023-06-28 15:17:35
ASP+XML制作菜单管理
2008-05-19 12:38:00
从传文件功能看本地化
2009-12-29 13:03:00
python基于物品协同过滤算法实现代码
2021-06-23 21:56:16
让MYSQL彻底支持中文
2008-12-24 16:23:00
asp如何用JMail POP3接收电子邮件?
2010-06-13 13:09:00
K最近邻算法(KNN)---sklearn+python实现方式
2023-09-14 15:29:31
![](https://img.aspxhome.com/file/2023/9/90769_0s.jpg)