Discussion:
Timezone in HH:MM Format
(too old to reply)
Ivan "Rambius" Ivanov
2024-06-18 23:32:17 UTC
Permalink
Hello,

How can I convert a date, usually datetime.now(), into a format where
the timezone is in hours:minutes format. I was able to get that format
in shell:

$ date +%Y-%m-%dT%H:%M:%S%:z
2024-06-18T19:24:09-04:00

The closest I got in python is

from datetime import datetime
from zoneinfo import ZoneInfo

s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
"%Y-%m-%dT%H:%M:%S%z")
print(s)

This prints the same as the shell command above except the last column:
2024-06-18T19:28:56-0400

Any help will be appreciated.

Regards
Ivan
--
Tangra Mega Rock: http://www.radiotangra.com
MRAB
2024-06-18 23:52:07 UTC
Permalink
Post by Ivan "Rambius" Ivanov
Hello,
How can I convert a date, usually datetime.now(), into a format where
the timezone is in hours:minutes format. I was able to get that format
$ date +%Y-%m-%dT%H:%M:%S%:z
2024-06-18T19:24:09-04:00
The closest I got in python is
from datetime import datetime
from zoneinfo import ZoneInfo
s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
"%Y-%m-%dT%H:%M:%S%z")
print(s)
2024-06-18T19:28:56-0400
Starting from Python 3.12, you can use "%:z" in the format string. For
earlier versions of Python, you need to do some string slicing.
Jon Ribbens
2024-06-19 00:12:28 UTC
Permalink
Post by Ivan "Rambius" Ivanov
Hello,
How can I convert a date, usually datetime.now(), into a format where
the timezone is in hours:minutes format. I was able to get that format
$ date +%Y-%m-%dT%H:%M:%S%:z
2024-06-18T19:24:09-04:00
The closest I got in python is
from datetime import datetime
from zoneinfo import ZoneInfo
s = datetime.strftime(datetime.now(ZoneInfo("America/New_York")),
"%Y-%m-%dT%H:%M:%S%z")
print(s)
2024-06-18T19:28:56-0400
Any help will be appreciated.
datetime.now(ZoneInfo("America/New_York")).isoformat()
Ivan "Rambius" Ivanov
2024-06-19 02:15:25 UTC
Permalink
Thank you all for your responses!

On Tue, Jun 18, 2024 at 9:54 PM Jon Ribbens via Python-list
Post by Jon Ribbens
datetime.now(ZoneInfo("America/New_York")).isoformat()
Both .isoformat() and "%:z" work.
--
Tangra Mega Rock: http://www.radiotangra.com
Loading...