How can I use the "datetime" class to get a different time zone than mine?
(for example gmt = 0 or utc+2)

Thanks!

Recommended Answers

Here is an example ...

# show the time in a different time zone
# see also: http://worldtimezone.net/

import datetime as dt

now = dt.datetime.now()

# for instance Los Angeles California
# runs 8 hours behind GMT time
gmt_offset …
Jump to Post

All 2 Replies

Here is an example ...

# show the time in a different time zone
# see also: http://worldtimezone.net/

import datetime as dt

now = dt.datetime.now()

# for instance Los Angeles California
# runs 8 hours behind GMT time
gmt_offset = -8
gmt = dt.timedelta(hours=gmt_offset)

print("Current L.A. time = %s" % now.time())
print("Current GMT time  = %s" % (now - gmt).time())

''' my output -->
Current L.A. time = 12:44:24.181000
Current GMT time  = 20:44:24.181000
'''

You might have to allow for daylight savings time.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.