Change time format in Django Views

♀尐吖头ヾ 提交于 2019-12-06 05:32:54

问题


importdatetime

in my django views to save the time in database

and

now = datetime.datetime.now()

when i am saving its value in database it returns something like

2013-04-28 22:54:30.223113

how can i remove

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

part please suggest how can i do this ...


回答1:


Set microsecond=0. But I can not find this feature in documentation.'

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



回答2:


You should ideally be using datetime field in your database. But if there is a constraint that you go to store date as a string use this to format it :

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



回答3:


You can do this way :

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

Look at this section section : http://docs.python.org/2/library/datetime.html#datetime.datetime.strftime

Have fun !



来源:https://stackoverflow.com/questions/16272564/change-time-format-in-django-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!