Skip to content

year() - Pine Script Function

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.

year(time, timezone) → series int
NameTypeDescription
timeseries intUNIX timestamp in milliseconds (often the built-in time series).
timezoneseries string (optional)Override timezone (e.g., "UTC", "America/New_York"). Defaults to syminfo.timezone.

series int — Year number (e.g., 2025).

  • 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() and dayofmonth() to assemble date strings or unique keys for analysis (year(time) * 100 + month(time)).
  • When you need session-based values, consider using time_tradingday as the input timestamp.
//@version=5
indicator("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")