python - 使用 strftime() 方法显示 Django DateTime 字段

标签 python django

我正在尝试格式化我的模型之一中包含的 DateTimeField 的字符串。

我在模型中使用此方法:

def HTMLDisplayTime(self):
        time = self.time.date.strftime(%a %H:%M  %d/%m/%y)
        return time

其中“self.time”指的是相关的 DateTimeField。

我尝试了几种变体,省略了一些项目,在 .strftime 方法之前有或没有 .date,并且导入了 import datetime > 和从日期时间导入日期时间

最佳答案

尝试在 strftime() 中的格式参数周围使用引号,例如:

time = self.time.strftime('%a %H:%M  %d/%m/%y')

关于python - 使用 strftime() 方法显示 Django DateTime 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470097/

相关文章:

python - 如何使用 openpyxl 将 Excel 作为电子邮件附件发送而不保存 [On The Fly]

Python 将 n-gram 从字典匹配到文本字符串

python - 计算 Python 中多维数组中达到或超过阈值的次数

django - 在 Django 管理中漂亮地打印 django.db.models.JSONField?

登录中的 Django 信号

python - django.core.exceptions.ValidationError : ["' ' is not a valid UUID.“]

python - 类型错误: 'float' 对象不可迭代,Python 列表

python - 加速 3D 点云中的 RANSAC 平面检测

python - 即使安装了 BeautifulSoap 的 ImportError

javascript - 更改引导导航栏中的事件类不保留在继承的 Django 模板中