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

monthOfYear() : Extract Month from Date


On this page

Summary

Extract the month component from a datetime value as a number (1-12).

Signature

monthOfYear(date)

Parameters

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

Returns

Type: Number (Integer)

The month as a number from 1 (January) to 12 (December).


Examples

Display Month Number

<p>Month: </p>

Data:

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

Output:

<p>Month: 3</p>

Quarter Calculation

<p>Quarter: Q</p>

Data:

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

Output:

<p>Quarter: Q3</p>

Seasonal Classification

<p>Season:

  Spring

  Summer

  Fall

  Winter

</p>

Data:

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

Output:

<p>Season: Summer</p>

Sales by Month

<h3>Monthly Sales</h3>

  <p>Month : $</p>

Data:

doc.Params["model"] = new {
    sales = new[] {
        new { date = new DateTime(2024, 1, 15), amount = 5000 },
        new { date = new DateTime(2024, 2, 15), amount = 6000 },
        new { date = new DateTime(2024, 3, 15), amount = 5500 }
    }
};

Output:

<h3>Monthly Sales</h3>
<p>Month 1: $5000</p>
<p>Month 2: $6000</p>
<p>Month 3: $5500</p>

Notes

  • Returns numeric value 1-12
  • 1 = January, 12 = December
  • Time component is ignored
  • For month names, use format(date, 'MMMM') or format(date, 'MMM')
  • Combine with conditional logic for seasonal or quarterly grouping
  • Use with yearOf() and dayOfMonth() for complete date parsing

See Also