Search code examples
pythondatetimepython-2.7pytz

Local Time To UTC time pytz


I converted python datetime with help of pytz.

Convertion is like this

2013-08-23T09:53:03 to 2013-08-23T15:23:03+05:30 (time is changed according timezone)

now the problem is "At at another loaction i get time as string 2013-08-23T15:23:03+05:30 how can i convert this string to 2013-08-23T09:53:03

thanks in advance


Solution

  • You can use the very useful dateutil package

    from dateutil import parser
    import pytz
    UTC = pytz.timezone('UTC')
    date = parser.parse("2013-08-23T15:23:03+05:30")
    dateutc = date.astimezone(UTC)
    print dateutc.isoformat() 
    # or user strptime to have in the format you want (without time zone)
    print dateutc.strftime("%Y-%m-%dT%H:%M:%S")