docx/field-date

A paragraph containing a simple DATE field with an explicit date-format switch.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/field-date
Formatdocx
Categoryfields
Spececma-376-5-part-1 § 17.16.5 w:fldSimple

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
date-field-simple-present
word/document.xml
exist
A <w:fldSimple> element whose w:instr starts with 'DATE' must be present in the body.
//w:fldSimple[starts-with(normalize-space(@w:instr), 'DATE')]
date-field-cached-value
word/document.xml
match = ^\d{4}-\d{2}-\d{2}$
The DATE field's cached run value must match an ISO-shaped yyyy-MM-dd date (the picture in \@).
//w:fldSimple[starts-with(normalize-space(@w:instr), 'DATE')]//w:t

Render assertions

IDPredicateSelectorpython-docxdocxjs
date-field-renderedcss_selector/ match-text = \d{4}-\d{2}-\d{2}
A renderer-exposed DATE field (or the fallback paragraph) must contain a date-shaped substring.
.docx-wrapper [data-field='DATE'], .docx-wrapper .field-date, .docx-wrapper ppass

Generator source

scripts/gen_field_date.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/field-date.docx``.

A paragraph containing a simple DATE field with an explicit ``\\@``
picture string and a cached ISO-shaped value. Designed to satisfy the
assertions in ``features/docx/field-date.json``.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "field-date.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph("Today: ")
    # Use python-docx's high-level Paragraph.add_simple_field(); cached
    # value '2026-05-04' matches the yyyy-MM-dd picture.
    paragraph.add_simple_field(r'DATE \@ "yyyy-MM-dd"', text="2026-05-04")
    _OUT.parent.mkdir(parents=True, exist_ok=True)
    doc.save(_OUT, reproducible=True)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/field-date",
  "title": "Field: DATE",
  "format": "docx",
  "category": "fields",
  "summary": "A paragraph containing a simple DATE field with an explicit date-format switch.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.16.5",
    "element": "w:fldSimple",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "The DATE field returns the current date; the \\@ switch supplies an explicit picture string. Simple-field form: <w:fldSimple w:instr='DATE \\@ \"yyyy-MM-dd\"'>. Cached value is carried as a <w:t> child run; Word refreshes it on open if the field is marked dirty."
  },
  "fixtures": {
    "machine": "docx/field-date",
    "office": "docx/field-date"
  },
  "generator": {
    "python": "scripts/gen_field_date.py"
  },
  "assertions": [
    {
      "id": "date-field-simple-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:fldSimple[starts-with(normalize-space(@w:instr), 'DATE')]",
      "must": "exist",
      "description": "A <w:fldSimple> element whose w:instr starts with 'DATE' must be present in the body."
    },
    {
      "id": "date-field-cached-value",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:fldSimple[starts-with(normalize-space(@w:instr), 'DATE')]//w:t",
      "must": "match",
      "value": "^\\d{4}-\\d{2}-\\d{2}$",
      "description": "The DATE field's cached run value must match an ISO-shaped yyyy-MM-dd date (the picture in \\@)."
    }
  ],
  "render_assertions": [
    {
      "id": "date-field-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper [data-field='DATE'], .docx-wrapper .field-date, .docx-wrapper p",
      "must": "match-text",
      "value": "\\d{4}-\\d{2}-\\d{2}",
      "description": "A renderer-exposed DATE field (or the fallback paragraph) must contain a date-shaped substring."
    }
  ]
}

Fixture

Download field-date.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/field-date page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

The DATE field returns the current date; the \@ switch supplies an explicit picture string. Simple-field form: <w:fldSimple w:instr='DATE \@ "yyyy-MM-dd"'>. Cached value is carried as a <w:t> child run; Word refreshes it on open if the field is marked dirty.