2

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Does strftime apply timezone? I just tried but it seems localtime is not applied. I get "2011-03-15 16:54:24" instead of "2011-03-16 01:54:24"
datetime.now() is localtime. datetime.utcnow() returns the UTC compliant form. strftime just specifies the partial output from that datetime object. If you need a specific region timezone you should do something like, import pytz datetime.now(pytz.timezone('Africa/Casablanca')).strftime('%Y-%m-%d %H:%M:%S')
0

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

2 Comments

Yes I'm using django. But what do you mean by localtime is correct?
by default the timezone is chicago, there's a line in settings.py where you can set your timezone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.