datetime issue

MRAB python at mrabarnett.plus.com
Sun Sep 16 11:27:16 EDT 2012


On 2012-09-16 09:25, Νικόλαος Κούρας wrote:
[snip]
>
> date = ( datetime.datetime.now() + datetime.timedelta(hours=8) ).strftime( '%y-%m-%d %H:%M:%S')
>
> but iam giving +8 hours which is the time difference between texas, us where the server sits and Greece's time.
>
> cant we somehow tell it to use GMT+2 ?
>
> also it would be nice if datetime.datetime.now(GMT+2) can be used.
>
In programming, you need attention to details.

My reply didn't use datetime.datetime.now(), it used 
datetime.datetime.utcnow().

datetime.datetime.now() gives the local time (local to the system on 
which it is running).

datetime.datetime.utcnow() gives the UTC (GMT) time, which is the same 
everywhere.

The line should be this:

date = (datetime.datetime.utcnow() + datetime.timedelta(hours=8) 
).strftime('%Y-%m-%d %H:%M:%S')

I've also used '%Y' instead of '%y' because I prefer 4 digits for the year.




More information about the Python-list mailing list