Previously:
If we have a datetime with timezone, we can also convert it to UTC datetime using pytz.
import pytz import datetime local_tz = pytz.timezone ("Asia/Hong_Kong") datetime_without_tz = datetime.datetime.strptime("2015-02-14 12:34:56", "%Y-%m-%d %H:%M:%S") datetime_with_tz = local_tz.localize(datetime_without_tz, is_dst=None) # No daylight saving time datetime_in_utc = datetime_with_tz.astimezone(pytz.utc) str1 = datetime_without_tz.strftime('%Y-%m-%d %H:%M:%S %Z') str2 = datetime_with_tz.strftime('%Y-%m-%d %H:%M:%S %Z') str3 = datetime_in_utc.strftime('%Y-%m-%d %H:%M:%S %Z') print 'Without Timzone : %s' % (str1) print 'With Timezone : %s' % (str2) print 'UTC Datetime : %s' % (str3)
Done =)
Reference: StackOverflow – How do I convert local time to UTC in Python?