Skip to content Skip to sidebar Skip to footer

Convert Time From Utc To Gmt With Python

I'm writting a python app that convert YML files to static HTML pages for my blog (static site generator). I want to add an RSS feed. On a some place I read that the publication da

Solution 1:

import datetime
basedate="2017-07-05T18:38:23+00:00"
formatfrom="%Y-%m-%dT%H:%M:%S+00:00"
formatto="%a %d %b %Y, %H:%M:%S GMT"print datetime.datetime.strptime(basedate,formatfrom).strftime(formatto)

will return you the correct transcription : Wed 05 Jul 2017, 18:38:23 GMT

my source for the formats : http://strftime.org/

Solution 2:

While the other answers are correct, I always use arrow to deal with date time in python, it's very easy to use.

For you example, you can simply do

import arrow
utc = arrow.utcnow()
local = utc.to('US/Pacific')
local.format('YYYY-MM-DD HH:mm:ss ZZ')

The doc contains more information.

Solution 3:

I strongly suggest using dateutil which is a really powerful extension to datetime. It has a robust parser, that enables parsing any input format of datetime to a datetime object. Then you can reformat it to your desired output format.

This should answer your need:

from dateutil import parser

original_date = parser.parse("2017-07-05T18:38:23+00:00")
required_date = original_date.strftime("%a %d %b %Y, %H:%M:%S GMT")

Post a Comment for "Convert Time From Utc To Gmt With Python"