python - 在函数python中返回值的问题
问题描述
我正在尝试返回当前时间,但由于某种原因我不能。我返回这个:
<function getHorarioActual at 0x000001E292E53950>
我的功能很简单
def getHorarioActual():
now = datetime.datetime.now()
horaActual = now.hour
minutoActual = now.minute
auxiliar = str(horaActual)+":"+str(minutoActual)
horarioActual = auxiliar
return horarioActual
解决方案
您不需要所有变量来格式化HH:MM
. 您的函数可以定义为:
from datetime import datetime
def getHorarioActual():
return datetime.now().strftime('%H:%M')
这是一个概念证明:
Python 3.7.5 (default, Oct 17 2019, 12:16:48)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> def getHorarioActual():
... return datetime.now().strftime('%H:%M')
...
>>> getHorarioActual()
'19:05'
>>>
推荐阅读
- reactjs - 如何部署完整的堆栈反应和弹簧启动以在互联网上看到?
- javascript - 如何使用方法映射显示一些内联元素?
- python - 用于计算数据框中没有均值的中位数的 Python 函数
- apache-spark - 在数据块中过滤 %[A-Za-z]%
- odata - OData 树与片段中的过滤器绑定
- python-3.x - 在 pandas 中将 datetime 64ns 转换为 mm/dd/yyyy
- react-native - 为什么我的应用程序在从 API 获取数据后无法导航到下一个屏幕?
- scala - Scala 部分统一推理符号/语法解释
- python - Python 负时差
- docker - 将 hasura 数据从云导出到 docker?