如何获取当前时间?
当前回答
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
示例输出:“2013-09-18 11:16:32”
请参阅strftime指令列表。
其他回答
如果您将其用于django datetime有时在服务器上无法工作,所以我建议使用时区
但要使用django时区,您应该在设置中设置您的国家/地区时区代码。py
TIME_ZONE = 'Asia/Tashkent'
那你就可以用它了
from django.utils import timezone
timezone.now() // for date time
timezone.now().year // for yaer
timezone.now().month // for month
timezone.now().day // for day
timezone.now().date // for date
timezone.now().hour // for hour
timezone.now().weekday // for minute
或者如果您想在python上使用
import time
time.strftime('%X') // '13:12:47'
time.strftime('%x') // '01/20/22'
time.strftime('%d') // '20' day
time.strftime('%m') // '01' month
time.strftime('%y') // '20' year
time.strftime('%H') // '01' hour
time.strftime('%M') // '01' minute
time.strftime('%m') // '01' second
如果您需要用于计时函数调用的时间,那么您需要time.perf_counter()。
start_time = time.perf_counter()
expensive_function()
time_taken = time.perf_counter() - start_time
print(f'expensive_function() took {round(time_taken,2)}s')
time.perf_counter()→ 浮动返回性能计数器的值(以秒为单位),即具有最高可用分辨率的时钟,以测量短持续时间。它确实包括了睡眠期间的时间,并且是系统范围内的。返回值的引用点未定义,因此只有连续调用结果之间的差异才有效。3.3版新增。time.perf_counter_ns()→ 整数与perf_counter()类似,但返回时间为纳秒。3.7版新增。
https://docs.python.org/3/library/time.html#time.perf_counter
我们可以使用datetime模块来完成
>>> from datetime import datetime
>>> now = datetime.now() #get a datetime object containing current date and time
>>> current_time = now.strftime("%H:%M:%S") #created a string representing current time
>>> print("Current Time =", current_time)
Current Time = 17:56:54
此外,我们可以使用pytZ模块获取当前时间zome。
>>> from pytz import timezone
>>> import pytz
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> amsterdam = timezone('Europe/Amsterdam')
>>> datetime_eu = datetime.now(amsterdam)
>>> print("Europe time::", datetime_eu.strftime("%H:%M:%S"))
Europe time:: 14:45:31
Do
from time import time
t = time()
t-浮点数,适用于时间间隔测量。
Unix和Windows平台有一些不同。
要在11:34:23.751毫秒内精确获得3个小数点,请运行以下命令:
def get_time_str(decimal_points=3):
return time.strftime("%H:%M:%S", time.localtime()) + '.%d' % (time.time() % 1 * 10**decimal_points)
更多上下文:
我想用毫秒来计算时间。获取它们的简单方法:
import time, datetime
print(datetime.datetime.now().time()) # 11:20:08.272239
# Or in a more complicated way
print(datetime.datetime.now().time().isoformat()) # 11:20:08.272239
print(datetime.datetime.now().time().strftime('%H:%M:%S.%f')) # 11:20:08.272239
# But do not use this:
print(time.strftime("%H:%M:%S.%f", time.localtime()), str) # 11:20:08.%f
但我只需要几毫秒,对吧?获取它们的最短方法:
import time
time.strftime("%H:%M:%S", time.localtime()) + '.%d' % (time.time() % 1 * 1000)
# 11:34:23.751
在最后一次乘法中添加或删除零以调整小数点的数量,或仅执行以下操作:
def get_time_str(decimal_points=3):
return time.strftime("%H:%M:%S", time.localtime()) + '.%d' % (time.time() % 1 * 10**decimal_points)
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 在Python中重置生成器对象