SeniorLee
SeniorLee

Reputation: 815

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.398503+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?

Upvotes: 2

Views: 906

Answers (2)

theheadofabroom
theheadofabroom

Reputation: 22019

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

Upvotes: 0

Iacks
Iacks

Reputation: 3845

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

Upvotes: 2

Related Questions