【问题标题】:Formatting date in Web2py Python在 Web2py Python 中格式化日期
【发布时间】:2012-09-26 16:08:04
【问题描述】:

我正在寻找一个函数来格式化日期以获得日、月和年。日期以以下格式 2012-09-26 存储在我的数据库中。

【问题讨论】:

  • 所以你有 YYYY-MM-DD,你的输出格式应该是什么?
  • 你想在模板中显示还是用例是什么?
  • 是的,我需要在模板中分别显示日、月和年。谢谢回复!
  • 日期是存储为SQL日期类型还是字符串?

标签: python database date web2py


【解决方案1】:

如果你的目标是在 web2py 模板上显示,那么你必须使用纯 Python 来格式化

{{=row.datetime_field.strftime("%d/%m/%Y")}}

上面会生成25/09/2012

看看 Python strftime 文档。

如果您只想显示日期。

{{=row.datetime_field.date}}

您也可以将其设置为该字段的表示

db.mytable.datetime_field.represent = lambda value, row: value.strftime("format-here")

该表示仅在 SQLFORM.grid 和 SQLTABLE 上有用

您只需要strftime

【讨论】:

  • 感谢您的回答!!这就是我一直在寻找的。​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
相关资源
最近更新 更多