如何获取当前时间?
当前回答
试试这个:-
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
其他回答
获取当前时间并将其转换为字符串:
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
import datetime
date_time = datetime.datetime.now()
date = date_time.date() # Gives the date
time = date_time.time() # Gives the time
print date.year, date.month, date.day
print time.hour, time.minute, time.second, time.microsecond
执行dir(date)或包括包在内的任何变量。您可以获取与变量关联的所有属性和方法。
import datetime
print('date='+datetime.datetime.now().__str__().split(' ')[0]+' '+'time='+datetime.datetime.now().__str__().split(' ')[1]
由于Qt被广泛使用,
from PyQt5 import QDateTime
print(QDateTime.currentDateTime().__str__().split('(')[1].rstrip(')'))
使用熊猫来获取当前的时间,有点过分了眼前的问题:
import pandas as pd
print(pd.datetime.now())
print(pd.datetime.now().date())
print(pd.datetime.now().year)
print(pd.datetime.now().month)
print(pd.datetime.now().day)
print(pd.datetime.now().hour)
print(pd.datetime.now().minute)
print(pd.datetime.now().second)
print(pd.datetime.now().microsecond)
输出:
2017-09-22 12:44:56.092642
2017-09-22
2017
9
22
12
44
56
92693
时间模块可以导入各种时间信息,包括睡眠和其他类型的信息,包括当前时间类型
import time
time.strftime("%T", time.localtime())
输出应如下所示
05:46:33
11:22:56
13:44:55
22:33:44
00:00:00
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录