PINE LIBRARY
DateTime

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.
Step 2
Select a date format and/or time format enum to be used as an input.
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 ๐๐๐.๐๐๐๐๐๐_๐๐๐๐(๐๐๐๐, ๐๐๐๐๐๐, ๐๐๐๐๐ฃ๐๐๐).
LIMITATIONS
The library's ease-of-use comes at a few costs:
ADVANTAGES
There are benefits to utilizing this library instead of directly using ๐๐๐.๐๐๐๐๐๐_๐๐๐๐():
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:
Time Format 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.
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.
NOTES
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
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")
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)
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.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
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.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
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.