[英]Change time format in Django Views
import
日期時間
在我的django視圖中節省數據庫中的時間
和
now = datetime.datetime.now()
當我在數據庫中保存它的值時, 返回類似的東西
2013-04-28 22:54:30.223113
我怎么能刪除
223113自2013-04-28 22:54:30.223113
部分請建議我該怎么做...
理想情況下,您應該在數據庫中使用datetime字段。 但是如果有一個約束,你去存儲日期作為字符串使用它來格式化它:
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2013-04-29 12:17:55'
設置microsecond=0
。 但我在文檔中找不到這個功能。
>>> now = datetime.datetime.now().replace(microsecond=0)
>>> print now
2013-04-29 12:47:28
你可以這樣做:
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
玩得開心 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.