Setting timezone in Python
>>> import os, time
>>> time.strftime('%X %x %Z')
'12:45:20 08/19/09 CDT'
>>> os.environ['TZ'] = 'Europe/London'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'18:45:39 08/19/09 BST'
To get the specific values you've listed:>>> year = time.strftime('%Y')
>>> month = time.strftime('%m')
>>> day = time.strftime('%d')
>>> hour = time.strftime('%H')
>>> minute = time.strftime('%M')
See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type. How to set time zone to datetime now in Python?
Try this:
Before Python 3.6 you can use pytz (external package)
from datetime import datetime
from pytz import timezone
print(datetime.now(timezone('Europe/London')))
Update according @WolfgangKuehn comment
Starting from Python 3.6 you can use zoneinfo via standard library (>=3.9) or backport instead of pytz
from datetime import datetime
from zoneinfo import ZoneInfo
print(datetime.now(ZoneInfo('Europe/London')))
how to set timezone to eastern for DateTime module in python?
Your date is a "naive" datetime, it doesn't have a timezone (tz=None
).
Then you have to localize this datetime by setting a timezone. Use pytz
module to do that.
Here is an example :
from datetime import datetime
from pytz import timezone
# define date format
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
# define eastern timezone
eastern = timezone('US/Eastern')
# naive datetime
naive_dt = datetime.now()
# localized datetime
loc_dt = datetime.now(eastern)
print(naive_dt.strftime(fmt))
# 2015-12-31 19:21:00
print(loc_dt.strftime(fmt))
# 2015-12-31 19:21:00 EST-0500
Read pytz documentation for more information How to make a timezone aware datetime object
In general, to make a naive datetime timezone-aware, use the localize method:
import datetime
import pytz
unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)
now_aware = pytz.utc.localize(unaware)
assert aware == now_aware
For the UTC timezone, it is not really necessary to use localize
since there is no daylight savings time calculation to handle:now_aware = unaware.replace(tzinfo=pytz.UTC)
works. (.replace
returns a new datetime; it does not modify unaware
.)
Related Topics
Representing and Solving a Maze Given an Image
Detect Tap with Pyaudio from Live Mic
Return List of Items in List Greater Than Some Value
Rotating a Two-Dimensional Array in Python
Numpy Argsort - What Is It Doing
Os.Path.Dirname(_File_) Returns Empty
Running Multiple Bash Commands with Subprocess
How to Frame Two for Loops in List Comprehension Python
How to Upsert Pandas Dataframe to Microsoft SQL Server Table
Join Two Lists of Dictionaries on a Single Key
How to Specify Python Version Used to Create Virtual Environment
What Does 'Wb' Mean in This Code, Using Python
Python - Email Header Decoding Utf-8
Reading Binary Data from Stdin
Saving Plots (Axessubplot) Generated from Python Pandas with Matplotlib's Savefig
Statistics: Combinations in Python