Sidechair

str.format_time()

Browse the reference

Formats a UNIX timestamp in milliseconds as text, using a pattern and an optional time zone.

Try it in the editor

str.format_time turns a time value such as time or timenow into readable text. The default pattern is "yyyy-MM-dd'T'HH:mm:ssZ".

Patterns use letters: yyyy for the year, MM for the month, dd for the day, HH for hours on a 24-hour clock, mm for minutes and ss for seconds. Text in single quotes stays literal.

Syntax

Pine Script
str.format_time(time: series int, format: series string (optional), timezone: series string (optional)) -> series string

Arguments

NameTypeDefaultDescription
timeseries intrequiredUNIX time in milliseconds.
formatseries string"yyyy-MM-dd'T'HH:mm:ssZ"The date and time pattern.
timezoneseries stringthe chart's time zoneTime zone name such as "America/New_York", or an offset like "UTC+2".

Returns

series string: the formatted date and time; na if time is na.

Remarks

  • Without a timezone the exchange time zone of the chart is used.
  • An unknown time zone name stops the script with an error.

Example

Pine ScriptHour of the bar as textOpen in editor
//@version=6
indicator("Format time")
hourText = str.format_time(time, "HH", "America/New_York")
plot(str.tonumber(hourText), "NY hour", color = color.blue, style = plot.style_stepline)

See also

Runs on Sidechair