问题
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