Skip to content

Format times and dates with pattern letters in Pine Script

Format times and dates with pattern letters in Pine Script

Section titled “Format times and dates with pattern letters in Pine Script”

TL;DR
Combine pattern letters with str.format() so any timestamp can be rendered exactly the way your indicator or alert needs.

DifficultyBeginner
Time to implement10-15 min
CategoryStrings
//@version=5
indicator("Pattern letter demo", overlay=false)
fmt = str.format(
"Session starts {0,date,EEEE d MMM yyyy} at {0,time,HH:mm:ss}",
time("D")
)
label.new(bar_index, close, fmt)

Indicators and alerts rarely want timestamps in the default US format. Pattern letters let you define your own output: 24-hour time, ISO dates, day names, shortened months, you name it. With them, you can create consistent UI copy, logs, and alert messages that match your audience or trading journal.

  • The difference between format specifiers and pattern letters
  • How to combine pattern letters inside str.format() placeholders
  • Common patterns for dates, times, and combined strings
  • Handling literal text and whitespace inside placeholders
CallPurpose
str.format(template, value...)Builds strings from placeholders that can include pattern letters.
str.format_time(timestamp, timezone, pattern)Formats timestamps for specific time zones using pattern letters.
time(resolution)Supplies the timestamp you want to format.
time("D")Returns the start of the current exchange day—handy for date labels.
label.new(...)Displays formatted timestamps on the chart.
LetterMeaningExample
yYearyyyy2025
MMonth in yearMMMFeb
dDay in monthdd05
EDay of weekEEEETuesday
HHour (24h)HH18
hHour (12h)hh06
mMinutesmm09
sSecondsss32
aAM/PM markeraPM
zTime zone short namezzzEST
  1. Choose the timestamp you want to format
    Decide whether to use the bar time (time), session start (time("D")), or a request to another resolution.

    barStart = time
    sessionOpen = time("D")
  2. Compose pattern letters inside a placeholder
    Pattern letters appear after the date or time specifier inside the {index,type,pattern} placeholder.

    isoString = str.format(
    "{0,date,yyyy-MM-dd}T{0,time,HH:mm:ss}",
    barStart
    )
  3. Render the formatted string in your UI or alerts
    Display the result on chart elements or use it inside alertcondition() messages.

    label.new(bar_index, high, isoString, style=label.style_label_up)
    alertcondition(true, "ISO timestamp", isoString)
//@version=5
indicator("Timestamp formatter", overlay=true)
fmtIso = str.format(
"{0,date,yyyy-MM-dd} {0,time,HH:mm:ss}",
time
)
fmtFriendly = str.format(
"{0,date,EEEE, MMM d} at {0,time,h:mm a zzz}",
time("D")
)
label.new(bar_index, high, fmtIso, style=label.style_label_down, textcolor=color.white, bgcolor=color.new(color.green, 65))
label.new(bar_index, low, fmtFriendly, style=label.style_label_up, textcolor=color.white, bgcolor=color.new(color.blue, 65))
Why this works.
  • Pattern letters give you ISO-style and reader-friendly formats without manual string slicing.
  • Whitespace inside the placeholder is preserved exactly—control spacing inside the pattern.
  • Literal text wrapped in single quotes remains untouched (`'Session'` prints the word instead of interpreting letters.
  • Wrap literal text in single quotes inside the placeholder: {0,time,'Session starts at' HH:mm}.
  • Combine date and time placeholders if you want to mix language-specific day names with numeric times.
  • Use str.replace() if you need to localise month names or adjust punctuation after formatting.
  • For consistent time zone output, prefer str.format_time(timestamp, "exchange", pattern) or specify a time zone explicitly.

Ensure you include a format type (date or time) before the pattern letters, e.g. {0,time,HH:mm}. Omitting it throws a run-time error.

Whitespace outside the pattern letters is significant—" {0,time,HH:mm} " keeps spaces, but inside the pattern you must add spaces explicitly (' at ').

  • Pattern letters let you define custom date and time strings in Pine Script.
  • Place pattern letters after date or time specifiers inside str.format() placeholders.
  • Use literal quotes and whitespace intentionally to keep output readable.
  • Combine str.format() and alertcondition() for rich, informative notifications.

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