如何在Python中获取当前时间

所属网站分类: python基础 > 模块,库


作者:追梦骚年

链接:http://www.pythonheidong.com/blog/article/68/

来源:python黑洞网,专注python资源,python教程,python技术!

获取当前时间的模块/方法是什么?

获取日期加时间使用:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)

>>> print(datetime.datetime.now())
2018-07-29 09:17:13.812189

你也可以使用time.strftime()

>>> from time import gmtime, strftime
>>> strftime("%Y-%m-%d %H:%M:%S", gmtime())
'2009-01-05 22:14:39'

仅获取时间:

>>> datetime.datetime.now().time()
datetime.time(15, 8, 24, 78915)

>>> print(datetime.datetime.now().time())
09:17:51.914526
 

猜你喜欢

转载自www.cnblogs.com/fuchen9527/p/10793902.html