Skip to main content Link Search Menu Expand Document (external link)

yearOf() : Extract Year from Date


On this page

Summary

Extract the year component from a datetime value as a 4-digit number.

Signature

yearOf(date)

Parameters

Parameter Type Required Description
date DateTime Yes The datetime to extract the year from

Returns

Type: Number (Integer)

The year as a 4-digit number (e.g., 2024).


Examples

Display Year

<p>Copyright  Acme Corporation</p>

Data:

doc.Params["model"] = new {
    currentDate = new DateTime(2024, 3, 15)
};

Output:

<p>Copyright 2024 Acme Corporation</p>

Calculate Age

<p> is  years old</p>

Data:

doc.Params["model"] = new {
    name = "Alice",
    birthDate = new DateTime(1990, 5, 20),
    today = new DateTime(2024, 3, 15)
};

Output:

<p>Alice is 34 years old</p>

Group by Year

<h3>Orders by Year</h3>

  <p>: </p>

Data:

doc.Params["model"] = new {
    orders = new[] {
        new { orderDate = new DateTime(2023, 6, 15), total = 150.00 },
        new { orderDate = new DateTime(2023, 8, 20), total = 200.00 },
        new { orderDate = new DateTime(2024, 1, 10), total = 175.00 }
    }
};

Output:

<h3>Orders by Year</h3>
<p>2023: 150</p>
<p>2023: 200</p>
<p>2024: 175</p>

Fiscal Year Calculation

<p>Fiscal Year: </p>

Data:

doc.Params["model"] = new {
    date = new DateTime(2024, 8, 15)
};

Output:

<p>Fiscal Year: 2025</p>

Notes

  • Returns 4-digit year value
  • Time component is ignored
  • Useful for copyright notices, age calculations, and date grouping
  • Combine with monthOfYear() and dayOfMonth() to build custom date formats
  • For complete date formatting, use format() function

See Also