year() - Pine Script Function
year()
Section titled “year()”Overview
Section titled “Overview”year() extracts the calendar year from a UNIX timestamp. Like other time helpers, it defaults to the chart’s exchange timezone but allows overrides when you need a different reference.
Syntax
Section titled “Syntax”year(time, timezone) → series intParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
time | series int | UNIX timestamp in milliseconds (often the built-in time series). |
timezone | series string (optional) | Override timezone (e.g., "UTC", "America/New_York"). Defaults to syminfo.timezone. |
Returns
Section titled “Returns”series int — Year number (e.g., 2025).
Remarks
Section titled “Remarks”- On overnight markets the bar’s open might belong to the previous calendar day, so
year(time)can lag the trading-date year on the first bars of a session. - Use with
month()anddayofmonth()to assemble date strings or unique keys for analysis (year(time) * 100 + month(time)). - When you need session-based values, consider using
time_tradingdayas the input timestamp.
Example
Section titled “Example”//@version=5indicator("Yearly separator", overlay=true)
newYear = ta.change(year(time))plotshape(newYear, style=shape.labeldown, text="New Year", color=color.new(color.blue, 0), location=location.abovebar, textcolor=color.white)
bgcolor(year(time) % 2 == 0 ? color.new(color.gray, 92) : na, title="Alternate years shading")