开发者

Datetime string with timezone in Python?

开发者 https://www.devze.com 2023-02-17 03:43 出处:网络
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开

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

0

精彩评论

暂无评论...
验证码 换一张
取 消