ICT:Cargo - use of year field format
Handling Year-Only Data in Cargo
By default, MediaWiki Cargo does not have a dedicated "Year" field type. Instead, you should use the Date type and configure your forms and queries to handle partial dates.
1. Define the Cargo Table
In your table declaration, use the standard `Date` field type. Cargo is smart enough to handle dates with varying levels of precision (Year, Year-Month, or Full Date).
{{#cargo_declare:_table=Books
|Title=String
|ReleaseDate=Date
}}
2. Create the Input Form
When using Page Forms, use the `input type=year` parameter. This ensures users see a simple text box instead of a full calendar picker.
{{{field|ReleaseDate|input type=year}}}
3. Querying Year Data
Since the data is stored in a `Date` field, you can use SQL functions within a #cargo_query to extract or filter by year.
Filter by a specific year
Use the `YEAR()` function in your `where` clause:
{{#cargo_query:
|tables=Books
|fields=Title, ReleaseDate
|where=YEAR(ReleaseDate)=2024
}}
Display only the year in results
If you want to ensure the output only shows "2024" (and not "2024-00-00"), alias the field in your query:
{{#cargo_query:
|tables=Books
|fields=Title, YEAR(ReleaseDate)=Year
|format=table
}}
Sort by Date
Even if some records have full dates (2024-05-12) and others only have years (2024), Cargo will sort them chronologically:
{{#cargo_query:
|tables=Books
|fields=Title, ReleaseDate
|order by=ReleaseDate DESC
}}
Technical Note: Precision
Cargo automatically creates a hidden field called `FieldName__precision` (e.g., `ReleaseDate__precision`).
- 1: Full Date (YYYY-MM-DD)
- 2: Year and Month (YYYY-MM)
- 3: Year Only (YYYY)
You can use this field in advanced templates to change how the date is formatted for the reader.