Datetime string with timezone in Python?
I'm working on Google app engine with Python.
I have stored a datetime object in database and fetched it and applied timezone, and I saved it using template. The result is "2011-03-15 16:54:24.398开发者_开发知识库503+09:00"
, but what I want is: "2011-03-16 01:54:24"
(timezone applied string without millisecond - note that day is changed).
How can I do that?
Once you have the datetime object, use strftime, like so;
from datetime import datetime
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
http://docs.python.org/release/2.6.6/library/datetime.html#strftime-and-strptime-behavior
using strftime is encouraged, but make sure that your localtime is correct. This should be a setting in your app/site assuming you're using django, or you may have to do this manually
精彩评论