Linked Questions
46 questions linked to/from How to convert a UTC datetime to a local datetime using only standard library?
1 vote
2 answers
3k views
Converting time zone in Python [duplicate]
I have time since epoch and I am converting it to datetime. import datetime s = '1346114717' t = datetime.datetime.fromtimestamp(float(s)) is this correct? t is in which timezone? How can I convert ...
1 vote
1 answer
2k views
Converting Python datetime from UTC to PST [duplicate]
Using Python, I have a datetime object with the value (for example) datetime(2021, 12, 28, 22, 31, tzinfo=tzutc()) This prints as 2021-12-28 22:31:00+00:00 How do I display that in US/Pacific? I've ...
443 votes
26 answers
874k views
How to convert local time string to UTC?
How do I convert a datetime string in local time to a string in UTC time? I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future. ...
331 votes
16 answers
666k views
Convert UTC datetime string to local datetime
I've never had to convert time to and from UTC. Recently had a request to have my app be timezone aware, and I've been running myself in circles. Lots of information on converting local time to UTC, ...
393 votes
12 answers
844k views
Converting datetime.date to UTC timestamp in Python
I am dealing with dates in Python and I need to convert them to UTC timestamps to be used inside Javascript. The following code does not work: >>> d = datetime.date(2011,01,01) >>> ...
321 votes
13 answers
691k views
In Python, how do you convert a `datetime` object to seconds?
I have a bunch of datetime objects and I want to calculate the number of seconds since a fixed time in the past for each one (for example since January 1, 1970). import datetime t = datetime.datetime(...
320 votes
12 answers
726k views
Convert datetime to Unix timestamp and convert it back in python
I have dt = datetime(2013,9,1,11), and I would like to get a Unix timestamp of this datetime object. When I do (dt - datetime(1970,1,1)).total_seconds() I got the timestamp 1378033200. When ...
162 votes
20 answers
254k views
Python: Figure out local timezone
I want to compare UTC timestamps from a log file with local timestamps. When creating the local datetime object, I use something like: >>> local_time=datetime.datetime(2010, 4, 27, 12, 0, 0, ...
152 votes
10 answers
317k views
Python Timezone conversion
How do I convert a time to another timezone in Python?
77 votes
10 answers
74k views
Getting computer's UTC offset in Python
In Python, how do you find what UTC time offset the computer is set to?
84 votes
6 answers
114k views
How to get system timezone setting and pass it to pytz.timezone?
We can use time.tzname get a local timezone name, but that name is not compatible with pytz.timezone. In fact, the name returned by time.tzname is ambiguous. This method returns ('CST', 'CST') in my ...
64 votes
6 answers
90k views
Converting timezone-aware datetime to local time in Python
How do you convert a timezone-aware datetime object to the equivalent non-timezone-aware datetime for the local timezone? My particular application uses Django (although, this is in reality a generic ...
70 votes
7 answers
217k views
How do I print a datetime in the local timezone?
Let's say I have a variable t that's set to this: datetime.datetime(2009, 7, 10, 18, 44, 59, 193982, tzinfo=<UTC>) If I say str(t), i get: '2009-07-10 18:44:59.193982+00:00' How can I get a ...
71 votes
6 answers
102k views
pytz - Converting UTC and timezone to local time
I have a datetime in utc time zone, for example: utc_time = datetime.datetime.utcnow() And a pytz timezone object: tz = timezone('America/St_Johns') What is the proper way to convert utc_time to the ...
54 votes
11 answers
13k views
Get the Olson TZ name for the local timezone?
How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C's localtime call? This is the value overridden via TZ, by symlinking /etc/localtime, or setting a ...