How do I insert today's ISO date into the selected cell?

When I push Ctrl-;, LibreOffice Calc inserts the date with my current locale. Can I make it insert ISO date instead?

1 Like

The hotkey assignation are locale dependent. In my LO (set to Hungarian locale, but to english UI) the fixed date with ISO format is assigned to the key the Shift-Ctrl-D.


.
And the default Date fomat is the ISO format in the Default Cell style:

Thanks for the screenshots.

On my LibreOffice, I have English locale and English UI. “Insert Current Date” is bound to Ctrl-;.

Still, with Ctrl-;, LibreOffice inserts the English locale date string instead of the ISO date string.

“Kislev” is not an English month name. Please upload a real, ODF type sample file here. Maybe your file has some manual (direct) formattings in the target cells.

  1. My LibO 25.8.1.1 offers a choice between 28 “English” locales.
  2. Shortcuts depend on the keyboard layout. If your chosen UI-Langage assumes a keyboard layout different5 from what you actually have, you7 may need to assign better (apropiate) shortcuts for some actions.
  3. English-UK and even more English-USA use absurd, outdated, and misleading date formats by default.
  4. If you want English with UK spelling, but ISO date format, you can use English-Canada. That’s my setting.
  5. If your keyboard can’t correctly pass the preset Ctrl+; for date insertion (my case again) delete the shortcut assigtnment and define a better one (in my case: Ctrl+0). You can do this via >Tools>Customise>Keyboard.

@Zizi64: “Kislev” means “september” when using the Hebrew calendar.
There is an inconsistency. Probably the used cells have a different language setting for their number formats. (However, Hebrew should also use Hebrew characters then. I don’t understand this.)

See also tdf#161377.

1 Like

Insert today’s date implicitly iso-formattet:

from datetime.date import today
def insert_iso_date():
    doc = XSCRIPTCONTEXT.getDocument()
    doc.CurrentSelection.FormulaLocal = today().isoformat()

and for the basics:

sub insert_iso_date
    thisComponent.CurrentSelection.FormulaLocal = format( now , "yyyy-mm-dd")
end sub

bind one of them to whatever shortcut you like

2 Likes

Hello, @karolus !
A clever trick that could be refined to work correctly with text-formatted cells and different selection types.

Sub InsertIsoDateToActiveCell()
  Dim oCell
  oCell = GetActiveCell()
  If Not (oCell Is Nothing) Then
    oCell.NumberFormat = 0
    oCell.FormulaLocal = Format(Date(), "yyyy-mm-dd")
  End If
End Sub

The text of the GetActiveCell function can be taken, for example, here.

1 Like

I was aware of this ( a bit cumbersome ) »GetActiveCell« to cover the corner-cases where …CurrentSelection may fail, but after some Compare_ism I find no advantage eg. to:

sub insert_iso_dispatcher

  dim document   as object
  dim dispatcher as object
  document   = ThisComponent.CurrentController.Frame
  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

  dim args3(0) as new com.sun.star.beans.PropertyValue
  args3(0).Name = "StringName"
  args3(0).Value = format(now, "yyyy-mm-dd")

  dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())
end sub

The REAL STRANGE issue ( from my humble-python-point-of-view ) is:

in case the active cell is already in edit-mode !! :

  • the basic-code works as expected …
    * …but the equivalent python-code doesnt produce any output ( neither in the cell nor error-messages )

in conclusion:

  • it seems there is no advantage for use of GetActiveCell ??

therefore the slightly improved python-snippet:

from datetime.date import today

def insert_iso_date(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    sel = doc.CurrentSelection
    try:  # upper-left of **last** multiselection
        sel[-1][0,0].FormulaLocal = today().isoformat()
    except (AttributeError, TypeError, KeyError): # [upper-left] single cell[range]
        sel[0,0].FormulaLocal = today().isoformat()
    ## solved: # todo: why does python silently fail with single-cell in **edit-mode**

  

GetActiveCell provides a clear understanding of the current cell. :slight_smile:
If we select cells like this:

2025-10-01 130923

GetActiveCell will return B2.
By the way, in this case, Ctrl-; will store the value in B2 and format cell A1. :slight_smile:

Basic “interacts” with the user interface, Python, as far as I know, does not.

Would be completely unexpected.
But without a good description, in the form “this is Basic code that works … this is the python code that doesn’t … with this specific file … and this specific selection …” - it is not actionable.

1 Like

Fair enough!
the following basic-code works as expected, regardless the selected cell is in edit-mode (blinking text-cursor) … or not:

sub insert_iso_dispatcher

  dim document   as object
  dim dispatcher as object
  document   = ThisComponent.CurrentController.Frame
  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

  dim args3(0) as new com.sun.star.beans.PropertyValue
  args3(0).Name = "StringName"
  args3(0).Value = format(now, "yyyy-mm-dd")

  dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())
end sub

The very same translated to python:

from datetime.date import today
from com.sun.star.beans import PropertyValue as pv
def insert_iso_date():
    doc = XSCRIPTCONTEXT.getDocument()
    createUnoService = XSCRIPTCONTEXT.getComponentContext().ServiceManager.createInstance
    frame = doc.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    prop = pv(Name = "StringName", Value = today().isoformat())
    dispatcher.executeDispatch(frame, ".uno:EnterString", "", 0, (prop,))  

works now to my surprise the same way as the basic-code above :hushed:
and I have absolutly no clue about the difference to my tests this morning

2 Likes

Somebody may be interested in an enhanced solution working with UTC, and in a way to append the UTC-offset to the local standard time.

If the proceeding is often needed It might also be recommendable to create an extra toolbar for the purpose.
Given the context I would also like to emphasise that ordinary date-time values (ISO 8601 with a space in place uf the “T” delimiter) can be used as operands in arithmetic calculations based on automatic conversion. A possibly appended zone info must first be removed.
The date-time values which shall be inserted in the current cell may therefore be represented by strings as stamps instead of formatted Double values.

See attachment:
UTC_timerstampsDEMO.ods (18.0 KB)

The contained user code can easily be adapted to different needs. The names of routines should, however, be changed then

just in case youre interested in an enhanced solution:

from datetime import datetime as dt
from zoneinfo import ZoneInfo, available_timezones
local = ZoneInfo('localtime')
utc = ZoneInfo('UTC')

stamp = dt.now(tz=utc)

print( f"{stamp.isoformat() = }")
print( f"{stamp.astimezone(local).isoformat() = }" )

# stamp.isoformat() = '2025-10-01T16:18:02.397890+00:00'
# stamp.astimezone(local).isoformat() = '2025-10-01T18:18:02.397890+02:00'