• Re: Timezone in HH:MM Format

    From Jon Ribbens@21:1/5 to Ivan "Rambius" Ivanov on Wed Jun 19 00:12:28 2024
    On 2024-06-18, Ivan "Rambius" Ivanov <rambiusparkisanius@gmail.com> wrote:
    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.

    datetime.now(ZoneInfo("America/New_York")).isoformat()

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Ivan "Rambius" Ivanov via Python-li on Wed Jun 19 00:52:07 2024
    On 2024-06-19 00:32, Ivan "Rambius" Ivanov via Python-list wrote:
    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

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)