PINE LIBRARY

DateTime

287
Library with enums that can be used as script inputs to allow users to set their preferred date and/or time formats. The user-selected formats can be passed to the library functions (which use ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ() under the hood) to get formatted date and time strings from a UNIX time.



PREFACE
The target audience of this publication is users creating their own indicators/strategies.

Sometimes a date and/or time needs to be displayed to the user. As a Pine Coder, it is natural to focus our initial attention on the primary calculations or functions of a script, which can lead to the display format of dates and times being an afterthought. While it may not be crucial for the main use case of a script, increased customizability can help push indicators/strategies to the next level in the eyes of the user.

The purpose of this library is to provide an easy-to-use mechanism for allowing script users to choose the formats of dates and times that are displayed to them. Not only is this helpful for users from around the world who may be accustomed to different date/time formats, but it also makes it easier for the script author because it offloads the date/time formatting decision from the author to the user.



HOW TO USE

Step 1
Import the library. Replace <version> with the latest available version number for this library.
Pine Scriptยฎ
//@version=6
indicator("Example")
 
import n00btraders/DateTime/<version> as dt


Step 2
Select a date format and/or time format enum to be used as an input.
Pine Scriptยฎ
dateFormatInput = input.enum(dt.DateFormat.FORMAT_3, "Date format")
timeFormatInput = input.enum(dt.TimeFormat.TWENTY_FOUR_HOURS, "Time hours format")


Step 3
Pass the user's selection as the `format` parameter in the formatting functions from this library. The `timestamp` & `timezone` parameters can be any value that would otherwise be used in ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ(๐š๐š’๐š–๐šŽ, ๐š๐š˜๐š›๐š–๐šŠ๐š, ๐š๐š’๐š–๐šŽ๐šฃ๐š˜๐š—๐šŽ).
Pine Scriptยฎ
string formattedDate = dt.formatDate(timestamp, dateFormatInput, timezone)
string formattedTime = dt.formatTime(timestamp, timeFormatInput, timezone)




LIMITATIONS
The library's ease-of-use comes at a few costs:
  • Fixed date/time formats.
    Using the library's pre-defined date & time formats means that additional custom formats cannot be utilized. For example, this library does not include seconds or fractional seconds in formatted time strings. If a script's use case requires displaying the 'seconds' from a time of day, then ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ() must be used directly.
  • Fixed time zone offset format.
    The `formatTime()` function of this library can optionally add the time zone offset at the end of the time string, but the format of the offset cannot be specified. Note: if the default format for time zone offset is not sufficient, the Timezone library can be imported directly to get the time zone offset string in a preferred format.




ADVANTAGES
There are benefits to utilizing this library instead of directly using ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ():
  • Easy to use from the user's perspective.
    The date & time format enums provide a similar look and feel to the "Date format" and "Time hours format" options that already exist in the TradingView chart settings.
  • Easy to use from the author's perspective.
    The exported functions from this library are modeled to behave similarly to the ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ(๐š๐š’๐š–๐šŽ, ๐š๐š˜๐š›๐š–๐šŠ๐š, ๐š๐š’๐š–๐šŽ๐šฃ๐š˜๐š—๐šŽ) built-in function from Pine Script.
  • Format quarter of the year.
    The date formatting function from this library can display a fiscal quarter if it's included in the user-selected format. This is currently not possible with the built-in ๐šœ๐š๐š›.๐š๐š˜๐š›๐š–๐šŠ๐š_๐š๐š’๐š–๐šŽ().




EXPORTED ENUM TYPES
This section will list the available date/time formats that can be used as a script input. Each enum type has a detailed //@๐š๐šž๐š—๐šŒ๐š๐š’๐š˜๐š— description in the source code to help determine the best choice for your scripts.

Date Format Enums:
  • ๐™ณ๐šŠ๐š๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š
  • ๐™ณ๐šŠ๐š๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š๐™ณ๐šŠ๐šข๐™พ๐š๐š†๐šŽ๐šŽ๐š”๐™ฐ๐š‹๐š‹๐š›
  • ๐™ณ๐šŠ๐š๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š๐™ณ๐šŠ๐šข๐™พ๐š๐š†๐šŽ๐šŽ๐š”๐™ต๐šž๐š•๐š•
  • ๐™ฒ๐šž๐šœ๐š๐š˜๐š–๐™ณ๐šŠ๐š๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š

Supporting Date Enums:
  • ๐™ณ๐šŠ๐š๐šŽ๐™ฟ๐š›๐šŽ๐š๐š’๐šก



Time Format Enums:
  • ๐šƒ๐š’๐š–๐šŽ๐™ต๐š˜๐š›๐š–๐šŠ๐š

Supporting Time Enums:
  • ๐šƒ๐š’๐š–๐šŽ๐™ฐ๐š‹๐š‹๐š›๐šŽ๐šŸ๐š’๐šŠ๐š๐š’๐š˜๐š—
  • ๐šƒ๐š’๐š–๐šŽ๐š‚๐šŽ๐š™๐šŠ๐š›๐šŠ๐š๐š˜๐š›
  • ๐šƒ๐š’๐š–๐šŽ๐™ฟ๐š˜๐šœ๐š๐š๐š’๐šก



Note: all exported enums have custom titles for each field. This means that the supporting enums could also be exposed to the end-user as script inputs if necessary. The supporting enums are used as optional parameters in this library's formatting functions to allow further customizability.



EXPORTED FUNCTIONS

formatDate(timestamp, format, timezone, prefix, trim)
โ€ƒโ€ƒConverts a UNIX time into a date string formatted according to the selected `format`.
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒtimestamp (series int): A UNIX time.
โ€ƒโ€ƒโ€ƒโ€ƒformat (series DateFormat): A date format.
โ€ƒโ€ƒโ€ƒโ€ƒtimezone (series string): A UTC/GMT offset or IANA time zone identifier.
โ€ƒโ€ƒโ€ƒโ€ƒprefix (series DatePrefix): Optional day of week prefix.
โ€ƒโ€ƒโ€ƒโ€ƒtrim (series bool): Optional truncation of numeric month / day.
โ€ƒโ€ƒReturns: Calendar date string using the selected format.
โธปโธปโธปโธปโธปโธปโธปโธป
Required parameters: `timestamp`, `format`.

Note: there is a version of this function for each Date Format enum type. The only difference is the type of the `format` parameter.

Tip: hover over the `formatDate()` function in the Pine Editor to display useful details:
  • Function description
  • Parameter descriptions + default values
  • Example function usage




formatTime(timestamp, format, timezone, trim, separator, postfix, space, offset)
โ€ƒโ€ƒConverts a UNIX time into a formatted time string using the 24-hour clock or 12-hour clock.
โ€ƒโ€ƒParameters:
โ€ƒโ€ƒโ€ƒโ€ƒtimestamp (series int): A UNIX time.
โ€ƒโ€ƒโ€ƒโ€ƒformat (series TimeFormat): A time format.
โ€ƒโ€ƒโ€ƒโ€ƒtimezone (series string): A UTC/GMT offset or IANA time zone identifier.
โ€ƒโ€ƒโ€ƒโ€ƒtrim (series TimeAbbreviation): Optional truncation of the hour and minute portion.
โ€ƒโ€ƒโ€ƒโ€ƒseparator (series TimeSeparator): Optional time separator.
โ€ƒโ€ƒโ€ƒโ€ƒpostfix (series TimePostfix): Optional format for the AM/PM postfix.
โ€ƒโ€ƒโ€ƒโ€ƒspace (series bool): Optional space between the time and the postfix.
โ€ƒโ€ƒโ€ƒโ€ƒoffset (series bool): Optional UTC offset as a suffix.
โ€ƒโ€ƒReturns: Time of day string using the selected format.
โธปโธปโธปโธปโธปโธปโธปโธป
Required parameters: `timestamp`, `format`.

Note: the `trim`, `postfix`, and `space` optional parameters are not applicable and will be ignored when using the 24-hour clock (`format` = TimeFormat.TWENTY_FOUR_HOURS).

Tip: hover over the `formatTime()` function in the Pine Editor to display useful details:
  • Function description
  • Parameter descriptions + default values
  • Example function usage
  • Example outputs for combinations of TimeFormat.* enum values & optional parameters





NOTES
  • This library can be used in conjunction with the Timezone library to increase the usability of scripts that can benefit from allowing the user to input their preferred time zone.
  • Credits to HoanGhetti for publishing an informative Markdown resource which I referenced to create the formatted function descriptions that pop up when hovering over `formatDate()` and `formatTime()` function calls in the Pine Editor.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.