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 withstr.format()so any timestamp can be rendered exactly the way your indicator or alert needs.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Strings |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("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)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- 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
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
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. |
Popular Pattern Letters
Section titled “Popular Pattern Letters”| Letter | Meaning | Example |
|---|---|---|
y | Year | yyyy → 2025 |
M | Month in year | MMM → Feb |
d | Day in month | dd → 05 |
E | Day of week | EEEE → Tuesday |
H | Hour (24h) | HH → 18 |
h | Hour (12h) | hh → 06 |
m | Minutes | mm → 09 |
s | Seconds | ss → 32 |
a | AM/PM marker | a → PM |
z | Time zone short name | zzz → EST |
Implementation Blueprint
Section titled “Implementation Blueprint”-
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 = timesessionOpen = time("D") -
Compose pattern letters inside a placeholder
Pattern letters appear after thedateortimespecifier inside the{index,type,pattern}placeholder.isoString = str.format("{0,date,yyyy-MM-dd}T{0,time,HH:mm:ss}",barStart) -
Render the formatted string in your UI or alerts
Display the result on chart elements or use it insidealertcondition()messages.label.new(bar_index, high, isoString, style=label.style_label_up)alertcondition(true, "ISO timestamp", isoString)
Example Playbook
Section titled “Example Playbook”//@version=5indicator("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))- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Wrap literal text in single quotes inside the placeholder:
{0,time,'Session starts at' HH:mm}. - Combine
dateandtimeplaceholders 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.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”My placeholder returns an error
Section titled “My placeholder returns an error”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.
Why does whitespace disappear?
Section titled “Why does whitespace disappear?”Whitespace outside the pattern letters is significant—" {0,time,HH:mm} " keeps spaces, but inside the pattern you must add spaces explicitly (' at ').
Key Takeaways
Section titled “Key Takeaways”- Pattern letters let you define custom date and time strings in Pine Script.
- Place pattern letters after
dateortimespecifiers insidestr.format()placeholders. - Use literal quotes and whitespace intentionally to keep output readable.
- Combine
str.format()andalertcondition()for rich, informative notifications.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.