在Django Views中更改时间格式

时间:2013-04-29 06:35:38

标签: python django date format

import的日期时间

在我的 django观看次数中将时间保存在数据库

now = datetime.datetime.now()

当我保存其在数据库中的值时,返回类似

的内容
  

2013-04-28 22:54:30.223113

如何删除

  

223113 from 2013-04-28 22:54:30.223113

部分请建议我该怎么办...

3 个答案:

答案 0 :(得分:9)

理想情况下,您应该在数据库中使用datetime字段。但如果存在约束条件 你去存储日期作为字符串使用它来格式化它:

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
'2013-04-29 12:17:55'

答案 1 :(得分:4)

设置microsecond=0。但我在文档中找不到这个功能。'

>>> now = datetime.datetime.now().replace(microsecond=0)
>>> print now
2013-04-29 12:47:28

答案 2 :(得分:3)

你可以这样做:

import datetime
n = datetime.datetime.now() # you'll get the datetime you already have
n.strftime("%d/%m/%y %H:%M:%S") 
# Now you have the string of the datatime with the format 
# day/month/year hour:minute:seconde

请查看此部分:http://docs.python.org/2/library/datetime.html#datetime.datetime.strftime

玩得开心!