Skip to content

How to code a time with date input in TradingView's Pine Script?

How to code a time with date input in TradingView’s Pine Script?

Section titled “How to code a time with date input in TradingView’s Pine Script?”

TL;DR
Combine input.time() with timestamp() to capture user-selected moments and drive time-based logic in your scripts.

DifficultyBeginner
Time to implement10-15 min
CategoryInputs
//@version=5
indicator("Time input demo", overlay=true)
startTime = input.time(timestamp("2024-01-01T09:30:00"), "Start")
isAfter = time >= startTime
bgcolor(isAfter ? color.new(color.teal, 85) : na)
Tip. `input.time()` returns a Unix timestamp in milliseconds. Use `timestamp()` (or ISO strings in v5) to supply readable defaults.

Time inputs let users specify when scripts should start trading, draw lines, or highlight sessions. They’re ideal for backtest windows, launch dates, or marking events.

  • Creating time inputs with friendly defaults and tooltips
  • Converting user-selected timestamps into visual markers or logic triggers
  • Building date ranges with pairs of input.time()
  • Handling missing or future dates safely
CallPurpose
input.time(defval, title, tooltip, group, inline, confirm, timezone)Creates a time selector in the settings panel.
timestamp(year, month, day, hour, minute)Produces readable defaults for time inputs.
line.new, bgcolor, alertconditionCommon consumers of time input values.
input.sessionCombine with time input to build complex schedules.
  1. Create the input with a sensible default
    Use timestamp() or ISO strings for readability.

    startDate = input.time(timestamp("2024-01-01T00:00:00"), "Start date")
  2. Apply logic or drawing at the selected time
    Compare time against the input to trigger actions.

    if time == startDate
    label.new(bar_index, high, "Start", style=label.style_label_down)
  3. Optionally define end times or ranges
    Use two inputs to create date windows for analysis/backtesting.

    endDate = input.time(timestamp("2024-02-01T00:00:00"), "End date")
    inRange = time >= startDate and time <= endDate
//@version=5
indicator("Time input range", overlay=true)
startDate = input.time(timestamp("2023-01-01T09:30:00"), "Range start", tooltip="First bar to highlight")
endDate = input.time(timestamp("2023-01-31T16:00:00"), "Range end", tooltip="Last bar to highlight")
inRange = time >= startDate and time <= endDate
bgcolor(inRange ? color.new(color.orange, 80) : na)
if inRange and barstate.islastconfirmedhistory
alert("Current bar inside selected range")
Why this works.
  • Users can adjust the start/end timestamps without editing code.
  • The range logic highlights exactly the bars between the chosen dates.
  • Alerts leverage the same inputs for flexible scheduling.
  • Time inputs use the chart’s time zone unless you set the timezone argument explicitly.
  • Use confirm=true if a change should require user confirmation (prevents accidental recalculations).
  • Pair with input.session or input.bool to build more advanced schedules.
  • Guard against startDate > endDate by swapping or using math.min/max.

Why doesn’t my script trigger at the selected time?

Section titled “Why doesn’t my script trigger at the selected time?”

Check that you compare against the instrument’s exchange time (all Pine time functions are exchange-based). Also ensure the bar resolution is fine-grained enough to include the specified timestamp.

Can I allow users to pick only a date (no time)?

Section titled “Can I allow users to pick only a date (no time)?”

input.time() always stores milliseconds; simply default the time to 00:00 and ignore the time portion in your logic if only the date matters.

  • input.time() gives users direct control over when code should run or highlight data.
  • Use pairs of time inputs to define custom analysis windows or backtest periods.
  • Combine with visual cues (labels, backgrounds) and alerts for interactive workflows.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.