How can I add a UTC offset to ISO 8601 datestamp fields in LibreOffice Writer?

Insert>Field>More Fields opens up the “Fields” menu. Under the “Documents” submenu, in the “Format” section, there is an option at the bottom of the list called “Additional formats…” This opens a menu called Format Number. If you scroll down to the bottom of the “Format” section on this menu, there is an ISO 8601 option in the format:

1999-12-31T-13:37:46
YYYY-MM-DD"T"HH:MM:SS

I would like to include the time offset (Date and time with the offset - 2023-12-19T12:03:29−07:00). Is this possible in Writer?

Embedded Calc: 99653.odt (15.8 KB)

How about clicking on additional formats under Time and select YYYY-MM-DD"T"HH:MM:SS with an offset of -420 (minutes)? Cheers, Al
UTC_Offset420Minutes.odt (11.9 KB)

As already mentioned you can append an ISO conforming UTC offset manually. I assume, however, you want to get an automatic solution.
This can’t be done by formatting, because the offset itself must be determined first. There is no way to include the needed calculations into a format definition.
Moreover LibreOffice has (afaik) no ready-made means to get UTC and this way to get the offset as a difference. You would need to rely on some UserDefinedCode for the task.
Since I collected a related toolbox some time ago, I could use it to support help with your request adding a few applying subroutines (“macros”).
See attached example document which contains the code.
Disclaimer: The code is partly preliminary and not very thoroghly checked!
disask99653insertDateTimeWithUtcOffsetAsStringOPrAsTextfield.odt (15.8 KB)

It supports, however, the insertion of UTC-offset DateTimeStamps and of ordinary strings containing them as well.

1 Like

Hallo
of course its python:

from datetime import datetime as dt, UTC
from time import strftime

CURRENT = dt.now()
CURRENT_UTC = dt.now(tz=UTC)
stamp = f"{CURRENT:%Y-%m-%dT%H:%M:%S}{strftime('%z')}"
utc_stamp = f"{CURRENT_UTC:%Y-%m-%dT%H:%M:%S %Z}"

def current_with_offset():
    doc = XSCRIPTCONTEXT.getDocument()
    doc.CurrentSelection[0].String = stamp

def current_utc_stamp():
    doc = XSCRIPTCONTEXT.getDocument()
    doc.CurrentSelection[0].String = utc_stamp

the first prints the current local-datetime with the offset to utc:

2023-12-20T10:57:22+0100
# here with CET 

the second print current utc-datetime:

2023-12-20T09:57:22 UTC

To manage your python in context with LibreOffice install apso.oxt from here

ps: with python >= 3.12 its possible to use:

strftime('%:z') 

which shows +01:00 instead +0100