CINXE.COM

Concepts / Time

<!DOCTYPE html><html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><script> const url = window.location.href; const expectedUrl = url.replace( /^https:\/\/(?!beta)(\w+\.)?tradingview\.com/, 'https://www.tradingview.com', ); if (url !== expectedUrl) { window.location.replace(expectedUrl); } </script><meta name="astro-view-transitions-enabled" content="true"><meta name="astro-view-transitions-fallback" content="none"><link rel="icon" href="https://static.tradingview.com/static/images/favicon.ico"><link rel="icon" type="image/svg+xml" href="/pine-script-docs/favicon.svg"><title>Concepts / Time</title><meta name="og:title" content="Concepts / Time"><meta name="twitter:title" content="Concepts / Time"><meta name="description" content="Everything you need to know about Pine Script™."><meta name="og:description" content="Everything you need to know about Pine Script™."><meta name="twitter:description" content="Everything you need to know about Pine Script™."><meta name="keywords" content="tradingview, pine, script, indicators, strategies"><meta name="og:image" content="/pine-script-docs/meta-image.png"><meta name="twitter:image" content="/pine-script-docs/meta-image.png"><meta name="og:image:width" content="1200"><meta name="og:image:height" content="630"><meta name="og:url" content="https://www.tradingview.com/pine-script-docs/concepts/time/"><meta name="twitter:url" content="https://www.tradingview.com/pine-script-docs/concepts/time/"><meta name="og:type" content="website"><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@TradingView"><link rel="canonical" href="https://www.tradingview.com/pine-script-docs/concepts/time/"><link rel="sitemap" href="/pine-script-docs/sitemap-index.xml"><link rel="stylesheet" href="/pine-script-docs/_astro/index.CI8A0IeF.css"> <link rel="stylesheet" href="/pine-script-docs/_astro/index.BL2bgAvq.css"> <style>.tv-spinner{display:none;position:absolute;margin:0 auto;border:0 solid rgb(149 152 161 / 20%);border-radius:50%;border-top-color:var(--tv-spinner-color, #2962ff);border-left-color:var(--tv-spinner-color, #2962ff);animation:tv-spinner-container-rotate .9s linear infinite}.tv-spinner-shown{display:block}.tv-spinner-size-large{top:calc(50% - 32px);left:calc(50% - 32px);width:56px;height:56px;border-width:4px}@keyframes tv-spinner-container-rotate{to{transform:rotate(360deg)}}:root{--page-background-color: #fff;background-color:var(--page-background-color)}:root[data-theme=dark]{--page-background-color: #000}body{background-color:var(--page-background-color)}#redirect-link{display:flex;position:absolute;inset:0;align-items:center;justify-content:center}#spinner{display:block;position:absolute;opacity:1;transition:opacity .2s} .twitter-tweet:not(.twitter-tweet-rendered){padding:var(--tc-padding, 1em);border:1px solid var(--tc-border-color, #cfd9de)}.twitter-tweet:not(.twitter-tweet-rendered)>:first-child{margin-top:0}.twitter-tweet:not(.twitter-tweet-rendered)>:last-child{margin-bottom:0}lite-youtube{background-color:#000;position:relative;display:block;contain:content;background-position:center center;background-size:cover;cursor:pointer;max-width:720px}lite-youtube:before{content:attr(data-title);display:block;position:absolute;top:0;background-image:linear-gradient(180deg,#000000ab,#0000008a 14%,#00000026 54%,#0000000d 72%,#0000 94%);height:99px;width:100%;font-family:YouTube Noto,Roboto,Arial,Helvetica,sans-serif;color:#eee;text-shadow:0 0 2px rgba(0,0,0,.5);font-size:18px;padding:25px 20px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;box-sizing:border-box}lite-youtube:hover:before{color:#fff}lite-youtube:after{content:"";display:block;padding-bottom:56.25%}lite-youtube>iframe{width:100%;height:100%;position:absolute;top:0;left:0;border:0}lite-youtube>.lty-playbtn{display:block;width:100%;height:100%;background:no-repeat center/68px 48px;background-image:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 68 48"><path d="M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55c-2.93.78-4.63 3.26-5.42 6.19C.06 13.05 0 24 0 24s.06 10.95 1.48 16.26c.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26z" fill="red"/><path d="M45 24 27 14v20" fill="white"/></svg>');position:absolute;cursor:pointer;z-index:1;filter:grayscale(100%);transition:filter .1s cubic-bezier(0,0,.2,1);border:0}lite-youtube:hover>.lty-playbtn,lite-youtube .lty-playbtn:focus{filter:none}lite-youtube.lyt-activated{cursor:unset}lite-youtube.lyt-activated:before,lite-youtube.lyt-activated>.lty-playbtn{opacity:0;pointer-events:none}.lyt-visually-hidden{clip:rect(0 0 0 0);clip-path:inset(50%);height:1px;overflow:hidden;position:absolute;white-space:nowrap;width:1px}lite-youtube>iframe{all:unset!important;width:100%!important;height:100%!important;position:absolute!important;inset:0!important;border:0!important}lite-vimeo{font-size:10px;background-color:#000;position:relative;display:block;contain:content;background-position:center center;background-size:cover}lite-vimeo:after{content:"";display:block;padding-bottom:56.25%}lite-vimeo>iframe{all:unset!important;width:100%!important;height:100%!important;position:absolute!important;inset:0!important;border:0!important}lite-vimeo>.ltv-playbtn{content:"";position:absolute;inset:0;width:100%;background:transparent;outline:0;border:0;cursor:pointer}lite-vimeo>.ltv-playbtn:before{width:6.5em;height:4em;background:#172322bf;opacity:.8;border-radius:.25rem;transition:all .2s cubic-bezier(0,0,.2,1)}lite-vimeo>.ltv-playbtn:focus:before{outline:auto}lite-vimeo:hover>.ltv-playbtn:before{background-color:#00adef;background-color:var(--ltv-color, #00adef);opacity:1}lite-vimeo>.ltv-playbtn:after{border-style:solid;border-width:1em 0 1em 1.7em;border-color:transparent transparent transparent #fff}lite-vimeo>.ltv-playbtn:before,lite-vimeo>.ltv-playbtn:after{content:"";position:absolute;top:50%;left:50%;transform:translate3d(-50%,-50%,0)}lite-vimeo.ltv-activated:before,lite-vimeo.ltv-activated>.ltv-playbtn{cursor:unset;opacity:0;pointer-events:none} </style><script type="module" src="/pine-script-docs/_astro/hoisted.yX2nbPMa.js"></script></head> <body> <div id="search-content-blur" hidden></div> <script> /* eslint-disable @typescript-eslint/typedef */ window.ThemeProvider = (() => { function getCurrent() { return ( // eslint-disable-next-line no-restricted-syntax typeof localStorage !== 'undefined' && localStorage.getItem('tv-docs-theme') ); } const storedTheme = getCurrent(); const theme = storedTheme || (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'); document.documentElement.dataset.theme = theme === 'light' ? 'light' : 'dark'; document.documentElement.classList.toggle( 'theme-dark', theme === 'dark', ); // add support for ui-lib themes document.documentElement.classList.toggle( 'sl-theme-dark', theme === 'dark', ); // add support for shoelace themes return { updatePickers(themeArg = storedTheme || 'auto') { let currentTheme = themeArg; if (currentTheme === 'unknown') { currentTheme = getCurrent() || 'auto'; } document .querySelectorAll('docs-theme-select') .forEach((picker) => { const select = picker.querySelector('select'); if (select) select.value = currentTheme; /** @type {HTMLTemplateElement | null} */ const tmpl = document.querySelector(`#theme-icons`); const newIcon = tmpl && tmpl.content.querySelector('.' + currentTheme); if (newIcon) { const oldIcon = picker.querySelector('svg.label-icon'); if (oldIcon) { oldIcon.replaceChildren( ...newIcon.cloneNode(true).childNodes, ); } } }); }, }; })(); /* eslint-enable @typescript-eslint/typedef */ </script><template id="theme-icons"><svg width="16" height="16" viewBox="0 0 28 28" class="light" data-icon="theme/sun-28"> <symbol id="ai:local:theme/sun-28"><g fill="currentColor"><path d="M14 3h1.5v5H14V3Zm0 18h1.5v5H14v-5Zm12-5.5V14h-5v1.5h5ZM8 14v1.5H3V14h5Zm15.3-7-1-1-3.6 3.6 1.1 1 3.5-3.5ZM9.5 18.7l1.1 1.1-3.5 3.5-1-1 3.4-3.6ZM22 23.3l1-1-3.6-3.6-1 1.1 3.5 3.5ZM10.3 9.6l-1.1 1-3.5-3.5 1-1 3.6 3.5Z"/><path fill-rule="evenodd" d="M19 14.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-1.5 0a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" clip-rule="evenodd"/></g></symbol><use xlink:href="#ai:local:theme/sun-28"></use> </svg><svg width="16" height="16" viewBox="0 0 28 28" class="dark" data-icon="theme/moon-28"> <symbol id="ai:local:theme/moon-28"><path fill="currentColor" fill-rule="evenodd" d="M21 7.02A9.23 9.23 0 0 0 15.2 5 9.1 9.1 0 0 0 6 14c0 4.97 4.12 9 9.2 9a9.33 9.33 0 0 0 5.8-2.02A7 7 0 0 1 14.36 14 7 7 0 0 1 21 7.02Zm-3.95-.3a7.91 7.91 0 0 0-1.85-.22A7.6 7.6 0 0 0 7.5 14a7.6 7.6 0 0 0 7.7 7.5 8 8 0 0 0 1.85-.22A8.46 8.46 0 0 1 12.86 14c0-3.1 1.69-5.8 4.19-7.28Z" clip-rule="evenodd"/></symbol><use xlink:href="#ai:local:theme/moon-28"></use> </svg><svg width="16" height="16" viewBox="0 0 28 28" class="auto" data-icon="theme/system-28"> <symbol id="ai:local:theme/system-28"><path fill="currentColor" d="M8 4h1v2H8V4Zm0 9v2h1v-2H8Zm6-3V9h-2v1h2ZM3 9v1h2V9H3Zm5.5 3a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Zm4.2-6-.7-.7-1.4 1.4.7.7L12.7 6ZM5 13.7l-.7-.7 1.4-1.4.7.7L5 13.7Zm7.7-.7-.7.7-1.4-1.4.7-.7 1.4 1.4ZM4.3 6l.7-.7 1.4 1.4-.7.7L4.3 6Zm3 17 14-14 .8.7L8 23.7l-.7-.7Zm17.2-.1a3.5 3.5 0 0 1-3.4-5.9H21a4 4 0 1 0 3.5 5.9Z"/></symbol><use xlink:href="#ai:local:theme/system-28"></use> </svg></template> <div class="backdrop" data-mobile-menu-backdrop data-astro-cid-h2irkosh></div> <div class="menu-container" data-astro-cid-h2irkosh> <div class="header" data-astro-cid-h2irkosh> <div class="header-group" data-astro-cid-h2irkosh> <div id="version-select" data-astro-cid-kx7qoxgq> <label style="--sl-select-width: undefined; --sl-label-icon-size: 16px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Version</span> <select value="v6" data-has-border no-icon="true" data-astro-cid-lmznfliq> <option value="v6" selected="true" data-astro-cid-lmznfliq>Version 6</option><option value="v5" data-astro-cid-lmznfliq>Version 5</option><option value="v4" data-astro-cid-lmznfliq>Version 4</option><option value="v3" data-astro-cid-lmznfliq>Version 3</option> </select> <svg width="16" height="16" viewBox="0 0 24 24" class="icon caret" data-astro-cid-lmznfliq data-icon="theme/down-caret"> <symbol id="ai:local:theme/down-caret"><path fill="currentColor" d="M17 9.17a1 1 0 0 0-1.41 0L12 12.71 8.46 9.17a1 1 0 1 0-1.41 1.42l4.24 4.24a1.002 1.002 0 0 0 1.42 0L17 10.59a1.002 1.002 0 0 0 0-1.42Z"/></symbol><use xlink:href="#ai:local:theme/down-caret"></use> </svg> </label> </div> <docs-theme-select class="" data-astro-cid-3wpspbi7> <label style="--sl-select-width: 48px; --sl-label-icon-size: 28px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Theme</span> <svg width="28" height="28" viewBox="0 0 28 28" class="icon label-icon" data-button data-round data-astro-cid-lmznfliq data-icon="theme/system-28"> <use xlink:href="#ai:local:theme/system-28"></use> </svg> <select value="auto" data-button data-astro-cid-lmznfliq> <option value="dark" data-astro-cid-lmznfliq>&nbsp;&nbsp;Dark&nbsp;&nbsp;</option><option value="light" data-astro-cid-lmznfliq>&nbsp;&nbsp;Light&nbsp;&nbsp;</option><option value="auto" selected="true" data-astro-cid-lmznfliq>&nbsp;&nbsp;Auto&nbsp;&nbsp;</option> </select> </label> </docs-theme-select> <script> ThemeProvider.updatePickers('unknown'); </script> </div> <div class="header-group" data-astro-cid-h2irkosh> <div class="not-content" style="stroke-width:2px" data-astro-cid-pkzv2hgs> <button id="mobile-menu-back-button" title="Close menu" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-primary stvb-icon"> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-h2irkosh data-icon="theme/arrow-back"> <symbol id="ai:local:theme/arrow-back"><g fill="none"><g clip-path="url(#a)"><path stroke="var(--arrow-fill-color, #131722)" d="m17 20-6-6 6-6"/></g><defs><clipPath id="a"><path fill="#fff" d="M28 28H0V0h28z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/arrow-back"></use> </svg> </button> </div> </div> </div> <aside id="nav" class="keep-visible" style="--navbar-right-border-width: 0px" data-astro-cid-sa57sq6l> <div class="sidebar-viewport slick-scroll" data-astro-cid-sa57sq6l> <div class="sidebar" data-mobile data-astro-cid-sa57sq6l> <ul class="toc" aria-label="Docs sidebar" data-astro-cid-sa57sq6l> <li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/welcome" data-astro-cid-omxx3dey>Welcome to Pine Script™ v6</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Pine Script™ primer</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-steps" data-astro-cid-omxx3dey>First steps</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-indicator" data-astro-cid-omxx3dey>First indicator</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/next-steps" data-astro-cid-omxx3dey>Next steps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Language</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/execution-model" data-astro-cid-omxx3dey>Execution model</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/time-series" data-astro-cid-omxx3dey>Time series</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/script-structure" data-astro-cid-omxx3dey>Script structure</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/identifiers" data-astro-cid-omxx3dey>Identifiers</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/operators" data-astro-cid-omxx3dey>Operators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/variable-declarations" data-astro-cid-omxx3dey>Variable declarations</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/conditional-structures" data-astro-cid-omxx3dey>Conditional structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/loops" data-astro-cid-omxx3dey>Loops</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/type-system" data-astro-cid-omxx3dey>Type system</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/built-ins" data-astro-cid-omxx3dey>Built-ins</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/user-defined-functions" data-astro-cid-omxx3dey>User-defined functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/objects" data-astro-cid-omxx3dey>Objects</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/enums" data-astro-cid-omxx3dey>Enums</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/methods" data-astro-cid-omxx3dey>Methods</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/arrays" data-astro-cid-omxx3dey>Arrays</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/matrices" data-astro-cid-omxx3dey>Matrices</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/maps" data-astro-cid-omxx3dey>Maps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details open data-is-parent data-is-on-path data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Concepts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/backgrounds" data-astro-cid-omxx3dey>Backgrounds</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-coloring" data-astro-cid-omxx3dey>Bar coloring</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-plotting" data-astro-cid-omxx3dey>Bar plotting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-states" data-astro-cid-omxx3dey>Bar states</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/chart-information" data-astro-cid-omxx3dey>Chart information</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/colors" data-astro-cid-omxx3dey>Colors</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/fills" data-astro-cid-omxx3dey>Fills</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/inputs" data-astro-cid-omxx3dey>Inputs</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/levels" data-astro-cid-omxx3dey>Levels</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/libraries" data-astro-cid-omxx3dey>Libraries</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/lines-and-boxes" data-astro-cid-omxx3dey>Lines and boxes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/non-standard-charts-data" data-astro-cid-omxx3dey>Non-standard charts data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/other-timeframes-and-data" data-astro-cid-omxx3dey>Other timeframes and data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/plots" data-astro-cid-omxx3dey>Plots</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/repainting" data-astro-cid-omxx3dey>Repainting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/sessions" data-astro-cid-omxx3dey>Sessions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/strategies" data-astro-cid-omxx3dey>Strategies</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/tables" data-astro-cid-omxx3dey>Tables</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/text-and-shapes" data-astro-cid-omxx3dey>Text and shapes</a></li><li class="item" data-current data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/time" data-astro-cid-omxx3dey>Time</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/timeframes" data-astro-cid-omxx3dey>Timeframes</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Writing scripts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/style-guide" data-astro-cid-omxx3dey>Style guide</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/debugging" data-astro-cid-omxx3dey>Debugging</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/profiling-and-optimization" data-astro-cid-omxx3dey>Profiling and optimization</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/publishing" data-astro-cid-omxx3dey>Publishing scripts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/limitations" data-astro-cid-omxx3dey>Limitations</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>FAQ</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/general" data-astro-cid-omxx3dey>General</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/data-structures" data-astro-cid-omxx3dey>Data structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/functions" data-astro-cid-omxx3dey>Functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/indicators" data-astro-cid-omxx3dey>Indicators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/other-data-and-timeframes" data-astro-cid-omxx3dey>Other data and timeframes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/programming" data-astro-cid-omxx3dey>Programming</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/strings-and-formatting" data-astro-cid-omxx3dey>Strings and formatting</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/error-messages" data-astro-cid-omxx3dey>Error messages</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/release-notes" data-astro-cid-omxx3dey>Release notes</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Migration guides</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-6" data-astro-cid-omxx3dey>To Pine Script™ version 6</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-5" data-astro-cid-omxx3dey>To Pine Script™ version 5</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-4" data-astro-cid-omxx3dey>To Pine Script™ version 4</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-3" data-astro-cid-omxx3dey>To Pine Script™ version 3</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/where-can-i-get-more-information" data-astro-cid-omxx3dey>Where can I get more information?</a></li> </ul> <div class="toc-bottom" data-astro-cid-sa57sq6l></div> </div> </div> </aside> </div> <header class="header" data-astro-cid-d74r2unp> <nav role="navigation" aria-label="Main Navigation" data-astro-cid-d74r2unp> <mobile-menu-button id="mobile-menu-button-wc" data-astro-cid-oojooh3d> <!-- Annoyingly I need to wrap this. TODO: improve this --> <div id="mobile-menu-button-header" data-astro-cid-oojooh3d> <div class="not-content" style="" data-astro-cid-pkzv2hgs> <button title="Open navigation menu" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-secondary stvb-icon stvb-icon-force-color stvb-force-no-border"> <svg width="28" height="28" viewBox="0 0 24 24" data-astro-cid-oojooh3d data-icon="theme/bars"> <symbol id="ai:local:theme/bars"><path fill="currentColor" d="M3 8h18a1 1 0 1 0 0-2H3a1 1 0 0 0 0 2Zm18 8H3a1 1 0 0 0 0 2h18a1 1 0 0 0 0-2Zm0-5H3a1 1 0 0 0 0 2h18a1 1 0 0 0 0-2Z"/></symbol><use xlink:href="#ai:local:theme/bars"></use> </svg> </button> </div> </div> </mobile-menu-button> <div data-hide-when-search data-astro-cid-d74r2unp> <div class="header-logo" data-astro-cid-tycb33lk> <a href="/pine-script-docs/" aria-label="Home button" data-astro-cid-tycb33lk> <div class="documentation-logo logo" data-astro-cid-tycb33lk><svg width="188" height="44" viewBox="0 0 188 44" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M26.9989 28.0001L18.4281 20.7531L11.3382 23.2611C10.402 23.5923 9.60771 22.5041 10.2085 21.7134L21.0877 7.39502C21.4749 6.88536 22.2346 6.86567 22.6478 7.35459L34.7627 21.6923C35.1531 22.1543 35.0472 22.8544 34.5376 23.1802L26.9989 28.0001ZM32.4978 22.1105L27.1641 25.5207L19.7195 19.2258C19.1767 18.7669 18.4312 18.6305 17.7611 18.8676L13.8245 20.2602L21.9253 9.59841L32.4978 22.1105Z" fill="currentColor"/> <path d="M33.432 28.0842L38.3592 34.7471C38.7427 35.2657 38.3724 36 37.7274 36H6.27248C5.62746 36 5.25722 35.2657 5.64074 34.7471L10.8523 27.6996C10.9484 27.5697 11.0822 27.4725 11.2356 27.4214L17.6878 25.2707C17.9285 25.1904 18.1932 25.2314 18.3984 25.3806L26.2854 31.1166C26.5445 31.3051 26.8922 31.3173 27.1639 31.1475L32.3838 27.885C32.7316 27.6677 33.1881 27.7544 33.432 28.0842Z" fill="currentColor"/> <path d="M59.56 13.96C63.016 13.96 65.536 16.456 65.536 19.696C65.536 22.936 63.016 25.432 59.56 25.432H57.376V31H53.896V13.96H59.56ZM59.512 22.168C61.096 22.168 62.152 21.088 62.152 19.696C62.152 18.304 61.096 17.224 59.512 17.224H57.376V22.168H59.512ZM70.8636 14.944C70.8636 16.096 69.9276 17.032 68.7516 17.032C67.6236 17.032 66.6636 16.096 66.6636 14.944C66.6636 13.792 67.6236 12.856 68.7516 12.856C69.9276 12.856 70.8636 13.792 70.8636 14.944ZM67.1196 31V19H70.4076V31H67.1196ZM73.0849 31V19H76.3729V20.368C76.9969 19.408 78.2449 18.736 79.8049 18.736C82.7089 18.736 84.4369 20.848 84.4369 23.968V31H81.1489V24.52C81.1489 22.792 80.4049 21.736 79.0369 21.736C77.5249 21.736 76.3729 22.84 76.3729 25.144V31H73.0849ZM86.2041 25C86.2041 21.448 88.8681 18.736 92.5161 18.736C95.6601 18.736 98.4201 20.752 98.4201 24.64C98.4201 24.928 98.4201 25.264 98.3721 25.768H89.3961C89.5881 27.376 91.0041 28.264 92.5881 28.264C94.0761 28.264 95.1561 27.568 95.6841 26.752L98.1321 28.576C97.0281 30.184 95.0841 31.264 92.5641 31.264C88.9881 31.264 86.2041 28.792 86.2041 25ZM92.4201 21.448C91.1961 21.448 89.8281 22.072 89.5161 23.536H95.1321C94.8441 22.12 93.6441 21.448 92.4201 21.448ZM104.412 28.24L107.076 26.008C107.916 27.28 109.38 28.024 110.772 28.024C112.14 28.024 113.052 27.352 113.052 26.368C113.052 25.408 112.356 24.736 110.676 24.16L109.236 23.656C106.5 22.696 105.036 21.112 105.036 18.784C105.036 15.568 107.484 13.672 110.796 13.672C112.884 13.672 114.708 14.392 116.172 16.024L113.868 18.4C113.052 17.416 111.996 16.936 110.82 16.936C109.644 16.936 108.54 17.44 108.54 18.448C108.54 19.48 109.38 19.936 111.18 20.584L112.548 21.088C115.044 22 116.604 23.584 116.604 26.152C116.58 29.2 114.156 31.288 110.628 31.288C107.916 31.288 105.636 30.112 104.412 28.24ZM129.859 27.808C128.827 29.872 126.667 31.264 124.171 31.264C120.619 31.264 117.811 28.624 117.811 25C117.811 21.376 120.619 18.736 124.171 18.736C126.667 18.736 128.827 20.128 129.859 22.192L127.003 23.728C126.523 22.624 125.491 21.808 124.171 21.808C122.443 21.808 121.123 23.152 121.123 25C121.123 26.848 122.443 28.192 124.171 28.192C125.491 28.192 126.523 27.376 127.003 26.272L129.859 27.808ZM131.482 31V19H134.77V20.92C135.178 19.744 136.402 18.856 137.746 18.856C138.082 18.856 138.442 18.88 138.85 19V22.336C138.346 22.168 137.89 22.072 137.362 22.072C135.802 22.072 134.77 23.296 134.77 25.264V31H131.482ZM144.215 14.944C144.215 16.096 143.279 17.032 142.103 17.032C140.975 17.032 140.015 16.096 140.015 14.944C140.015 13.792 140.975 12.856 142.103 12.856C143.279 12.856 144.215 13.792 144.215 14.944ZM140.471 31V19H143.759V31H140.471ZM146.436 36.016V19H149.724V20.344C150.276 19.576 151.548 18.736 153.204 18.736C156.396 18.736 158.844 21.592 158.844 25C158.844 28.408 156.396 31.264 153.204 31.264C151.548 31.264 150.276 30.424 149.724 29.656V36.016H146.436ZM155.508 25C155.508 23.128 154.284 21.736 152.484 21.736C150.684 21.736 149.46 23.128 149.46 25C149.46 26.872 150.684 28.264 152.484 28.264C154.284 28.264 155.508 26.872 155.508 25ZM168.407 30.88C167.879 31.048 167.231 31.144 166.319 31.144C163.775 31.144 161.735 29.728 161.735 26.8V21.88H159.311V19H161.735V15.664H165.023V19H168.407V21.88H165.023V26.152C165.023 27.616 165.647 28.192 167.063 28.192C167.591 28.192 168.023 28.12 168.407 27.976V30.88ZM177.626 22.648V13.96H179.978L181.874 16.888L183.746 13.96H186.122V22.648H183.698V18.184L181.874 20.968L180.026 18.136V22.648H177.626ZM171.386 22.648V16.456H169.058V13.96H176.258V16.456H173.93V22.648H171.386Z" fill="currentColor"/> </svg> </div> </a> </div> </div> <div class="flex" data-astro-cid-d74r2unp></div> <div class="flex" data-astro-cid-d74r2unp> <div class="search-container" data-astro-cid-fg37foga> <div class="search-wrapper" data-astro-cid-fg37foga> <input class="search-input" type="text" placeholder="Search docs" name="s" value="" data-astro-cid-fg37foga> <svg width="28" height="28" viewBox="0 0 28 28" class="search-button" data-astro-cid-fg37foga data-icon="theme/search"> <symbol id="ai:local:theme/search"><path fill="currentColor" fill-rule="evenodd" d="M18.5 12.5a6 6 0 1 1-12 0 6 6 0 0 1 12 0Zm-1.25 5.8a7.5 7.5 0 1 1 1.06-1.06l4.22 4.23.53.53L22 23.06l-.53-.53-4.22-4.22Z" clip-rule="evenodd"/></symbol><use xlink:href="#ai:local:theme/search"></use> </svg> <button class="search-clear" type="button" title="Reset" data-search-clear data-astro-cid-fg37foga>Clear</button> <span class="divider" data-astro-cid-fg37foga></span> <button class="search-close" type="button" title="Close" data-search-close data-astro-cid-fg37foga> <svg width="18" height="18" viewBox="0 0 18 18" data-astro-cid-fg37foga data-icon="theme/cross-18"> <use xlink:href="#ai:local:theme/cross-18"></use> </svg> </button> </div> <div id="search-results-wrapper" data-astro-cid-fg37foga> <aside id="search-results" hidden data-astro-cid-fg37foga> <!-- Don't use h1 because when built it will be used as the first heading on the page --> <div class="heading" data-astro-cid-fg37foga>Search results</div> <div id="search-results-contents" data-astro-cid-fg37foga></div> </aside> </div> </div> </div> <ul class="links" data-astro-cid-d74r2unp> </ul> <div class="flex" data-astro-cid-d74r2unp></div> <div data-hide-when-search data-astro-cid-d74r2unp> <div id="version-select" data-hideable="true" data-astro-cid-kx7qoxgq> <label style="--sl-select-width: undefined; --sl-label-icon-size: 16px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Version</span> <select value="v6" data-has-border no-icon="true" data-astro-cid-lmznfliq> <option value="v6" selected="true" data-astro-cid-lmznfliq>Version 6</option><option value="v5" data-astro-cid-lmznfliq>Version 5</option><option value="v4" data-astro-cid-lmznfliq>Version 4</option><option value="v3" data-astro-cid-lmznfliq>Version 3</option> </select> <svg width="16" height="16" viewBox="0 0 24 24" class="icon caret" data-astro-cid-lmznfliq data-icon="theme/down-caret"> <use xlink:href="#ai:local:theme/down-caret"></use> </svg> </label> </div> </div> <button id="search-button" data-astro-cid-6zeqadij> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-6zeqadij data-icon="theme/search"> <use xlink:href="#ai:local:theme/search"></use> </svg> </button> <script data-base-url="/pine-script-docs"> async function loadPageFind() { const base = document.currentScript.getAttribute('data-base-url'); const pageFindBundleUrl = `${base}/pagefind/`; const pagefind = await import(`${pageFindBundleUrl}pagefind.js`); await pagefind.options({ bundlePath: pageFindBundleUrl, }); window.pagefind = pagefind; } loadPageFind().catch(); </script> <docs-theme-select class="hide-with-breakpoint-568" data-astro-cid-3wpspbi7> <label style="--sl-select-width: 48px; --sl-label-icon-size: 28px;" data-astro-cid-lmznfliq> <span class="sr-only" data-astro-cid-lmznfliq>Theme</span> <svg width="28" height="28" viewBox="0 0 28 28" class="icon label-icon" data-button data-round data-astro-cid-lmznfliq data-icon="theme/system-28"> <use xlink:href="#ai:local:theme/system-28"></use> </svg> <select value="auto" data-button data-astro-cid-lmznfliq> <option value="dark" data-astro-cid-lmznfliq>&nbsp;&nbsp;Dark&nbsp;&nbsp;</option><option value="light" data-astro-cid-lmznfliq>&nbsp;&nbsp;Light&nbsp;&nbsp;</option><option value="auto" selected="true" data-astro-cid-lmznfliq>&nbsp;&nbsp;Auto&nbsp;&nbsp;</option> </select> </label> </docs-theme-select> <script> ThemeProvider.updatePickers('unknown'); </script> </nav> </header> <div id="image-lightbox" class="not-content" hidden data-astro-cid-kws7taxh> <div class="button-wrapper" data-astro-cid-kws7taxh> <div class="not-content" style="" data-astro-cid-pkzv2hgs> <button id="lightbox-close-button" title="Close image preview" data-astro-cid-pkzv2hgs class="not-content stvb-base stvb-pointer stvb-black stvb-medium stvb-secondary stvb-icon stvb-force-no-border"> <svg width="24" height="24" viewBox="0 0 18 18" data-astro-cid-kws7taxh data-icon="theme/cross-18"> <symbol id="ai:local:theme/cross-18"><g fill="none"><g clip-path="url(#a)"><path fill="currentColor" fill-rule="evenodd" d="M5.53 4.47 4.47 5.53 7.94 9l-3.47 3.47 1.06 1.06L9 10.06l3.47 3.47 1.06-1.06L10.06 9l3.47-3.47-1.06-1.06L9 7.94 5.53 4.47Z" clip-rule="evenodd"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h18v18H0z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/cross-18"></use> </svg> </button> </div> </div> <img id="lightbox-image" src="" data-astro-cid-kws7taxh> </div> <div id="page-container" data-astro-cid-xgirumru> <aside id="nav" class="" style="" data-astro-cid-sa57sq6l> <div class="sidebar-viewport slick-scroll" data-astro-cid-sa57sq6l> <div class="sidebar" data-astro-cid-sa57sq6l> <ul class="toc" aria-label="Docs sidebar" data-astro-cid-sa57sq6l> <li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/welcome" data-astro-cid-omxx3dey>Welcome to Pine Script™ v6</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Pine Script™ primer</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <symbol id="ai:local:theme/right-caret"><path fill="currentColor" d="m14.83 11.29-4.24-4.24a1 1 0 1 0-1.42 1.41L12.71 12l-3.54 3.54a1 1 0 0 0 0 1.41 1 1 0 0 0 .71.29 1 1 0 0 0 .71-.29l4.24-4.24a1.002 1.002 0 0 0 0-1.42Z"/></symbol><use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-steps" data-astro-cid-omxx3dey>First steps</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/first-indicator" data-astro-cid-omxx3dey>First indicator</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/primer/next-steps" data-astro-cid-omxx3dey>Next steps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Language</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/execution-model" data-astro-cid-omxx3dey>Execution model</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/time-series" data-astro-cid-omxx3dey>Time series</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/script-structure" data-astro-cid-omxx3dey>Script structure</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/identifiers" data-astro-cid-omxx3dey>Identifiers</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/operators" data-astro-cid-omxx3dey>Operators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/variable-declarations" data-astro-cid-omxx3dey>Variable declarations</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/conditional-structures" data-astro-cid-omxx3dey>Conditional structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/loops" data-astro-cid-omxx3dey>Loops</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/type-system" data-astro-cid-omxx3dey>Type system</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/built-ins" data-astro-cid-omxx3dey>Built-ins</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/user-defined-functions" data-astro-cid-omxx3dey>User-defined functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/objects" data-astro-cid-omxx3dey>Objects</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/enums" data-astro-cid-omxx3dey>Enums</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/methods" data-astro-cid-omxx3dey>Methods</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/arrays" data-astro-cid-omxx3dey>Arrays</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/matrices" data-astro-cid-omxx3dey>Matrices</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/language/maps" data-astro-cid-omxx3dey>Maps</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details open data-is-parent data-is-on-path data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Concepts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/backgrounds" data-astro-cid-omxx3dey>Backgrounds</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-coloring" data-astro-cid-omxx3dey>Bar coloring</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-plotting" data-astro-cid-omxx3dey>Bar plotting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/bar-states" data-astro-cid-omxx3dey>Bar states</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/chart-information" data-astro-cid-omxx3dey>Chart information</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/colors" data-astro-cid-omxx3dey>Colors</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/fills" data-astro-cid-omxx3dey>Fills</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/inputs" data-astro-cid-omxx3dey>Inputs</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/levels" data-astro-cid-omxx3dey>Levels</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/libraries" data-astro-cid-omxx3dey>Libraries</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/lines-and-boxes" data-astro-cid-omxx3dey>Lines and boxes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/non-standard-charts-data" data-astro-cid-omxx3dey>Non-standard charts data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/other-timeframes-and-data" data-astro-cid-omxx3dey>Other timeframes and data</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/plots" data-astro-cid-omxx3dey>Plots</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/repainting" data-astro-cid-omxx3dey>Repainting</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/sessions" data-astro-cid-omxx3dey>Sessions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/strategies" data-astro-cid-omxx3dey>Strategies</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/tables" data-astro-cid-omxx3dey>Tables</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/text-and-shapes" data-astro-cid-omxx3dey>Text and shapes</a></li><li class="item" data-current data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/time" data-astro-cid-omxx3dey>Time</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/concepts/timeframes" data-astro-cid-omxx3dey>Timeframes</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Writing scripts</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/style-guide" data-astro-cid-omxx3dey>Style guide</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/debugging" data-astro-cid-omxx3dey>Debugging</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/profiling-and-optimization" data-astro-cid-omxx3dey>Profiling and optimization</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/publishing" data-astro-cid-omxx3dey>Publishing scripts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/writing/limitations" data-astro-cid-omxx3dey>Limitations</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>FAQ</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/general" data-astro-cid-omxx3dey>General</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/alerts" data-astro-cid-omxx3dey>Alerts</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/data-structures" data-astro-cid-omxx3dey>Data structures</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/functions" data-astro-cid-omxx3dey>Functions</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/indicators" data-astro-cid-omxx3dey>Indicators</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/other-data-and-timeframes" data-astro-cid-omxx3dey>Other data and timeframes</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/programming" data-astro-cid-omxx3dey>Programming</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/faq/strings-and-formatting" data-astro-cid-omxx3dey>Strings and formatting</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/error-messages" data-astro-cid-omxx3dey>Error messages</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/release-notes" data-astro-cid-omxx3dey>Release notes</a></li><li class="item" data-astro-cid-omxx3dey><details data-astro-cid-omxx3dey><summary data-astro-cid-omxx3dey><div class="summary-link" data-astro-cid-omxx3dey>Migration guides</div><div class="caret" data-astro-cid-omxx3dey><svg width="18" height="18" viewBox="0 0 24 24" class="icon" data-astro-cid-omxx3dey data-icon="theme/right-caret"> <use xlink:href="#ai:local:theme/right-caret"></use> </svg></div></summary><ul class="children" data-astro-cid-omxx3dey><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-6" data-astro-cid-omxx3dey>To Pine Script™ version 6</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-5" data-astro-cid-omxx3dey>To Pine Script™ version 5</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-4" data-astro-cid-omxx3dey>To Pine Script™ version 4</a></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/migration-guides/to-pine-version-3" data-astro-cid-omxx3dey>To Pine Script™ version 3</a></li></ul></details></li><li class="item" data-astro-cid-omxx3dey><a class="page-link" href="/pine-script-docs/where-can-i-get-more-information" data-astro-cid-omxx3dey>Where can I get more information?</a></li> </ul> <div class="toc-bottom" data-astro-cid-sa57sq6l></div> </div> </div> </aside> <main class="main-pane" data-page-type="md" data-astro-cid-xgirumru> <a id="top" data-astro-cid-xgirumru></a> <main class="content" data-toc-shown data-astro-cid-ju3wuhkz> <div class="content-width" data-astro-cid-xgirumru> <div class="breadcrumbs" data-pagefind-ignore data-astro-cid-wlavna2o> <a href="/pine-script-docs" aria-label="Return back to the documentation home page." data-astro-cid-wlavna2o> <span data-astro-cid-wlavna2o>User Manual</span> </a> <span class="divider" data-astro-cid-wlavna2o>/</span> <a href="/pine-script-docs/concepts/alerts" data-astro-cid-wlavna2o>Concepts</a> <span class="divider" data-astro-cid-wlavna2o>/</span> <span class="current-item" data-astro-cid-wlavna2o>Time</span> </div> </div> <div id="slot-container" data-astro-cid-xgirumru> <h1 id="time" class="md-heading"><a href="#time">Time<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h1> <h2 id="introduction" class="md-heading"><a href="#introduction">Introduction<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>In Pine Script™, the following key aspects apply when working with date and time values:</p> <ul> <li><strong>UNIX timestamp</strong>: The native format for time values in Pine, representing the absolute number of <em>milliseconds</em> elapsed since midnight <a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time" rel="nofollow">UTC</a> on 1970-01-01. Several built-ins return UNIX timestamps directly, which users can <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">format</a> into readable dates and times. See the <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> section below for more information.</li> <li><strong>Exchange time zone</strong>: The <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a> of the instrument’s exchange. All <a href="/pine-script-docs/concepts/time/#calendar-based-variables">calendar-based variables</a> hold values expressed in the exchange time zone, and all built-in function overloads that have a <code dir="auto">timezone</code> parameter use this time zone by default.</li> <li><strong>Chart time zone</strong>: The time zone the chart and <a href="/pine-script-docs/writing/debugging/#pine-logs">Pine Logs</a> message prefixes use to express time values. Users can set the chart time zone using the “Timezone” input in the “Symbol” tab of the chart’s settings. This setting only changes the <em>display</em> of dates and times on the chart and the times that prefix logged messages. It does <strong>not</strong> affect the behavior of Pine scripts because they cannot access a chart’s time zone information.</li> <li><strong><code dir="auto">timezone</code> parameter</strong>: A “string” parameter of time-related functions that specifies the time zone used in their calculations. For <a href="/pine-script-docs/concepts/time/#calendar-based-functions">calendar-based functions</a>, such as <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a>, the <code dir="auto">timezone</code> parameter determines the time zone of the returned value. For functions that return UNIX timestamps, such as <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a>, the specified <code dir="auto">timezone</code> defines the time zone of other applicable parameters, e.g., <code dir="auto">session</code>. See the <a href="/pine-script-docs/concepts/time/#time-zone-strings">Time zone strings</a> section to learn more.</li> </ul> <h2 id="unix-timestamps" class="md-heading"><a href="#unix-timestamps">UNIX timestamps<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p><a href="https://en.wikipedia.org/wiki/Unix_time" rel="nofollow">UNIX time</a> is a standardized date and time representation that measures the number of <em>non-leap seconds</em> elapsed since January 1, 1970 at 00:00:00 <a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time" rel="nofollow">UTC</a> (the <em>UNIX Epoch</em>), typically expressed in seconds or smaller time units. A UNIX time value in Pine Script™ is an “int” <em>timestamp</em> representing the number of <em>milliseconds</em> from the UNIX Epoch to a specific point in time.</p> <p>Because a UNIX timestamp represents the number of consistent time units elapsed from a fixed historical point (epoch), its value is <strong>time zone-agnostic</strong>. A UNIX timestamp in Pine always corresponds to the same distinct point in time, accurate to the millisecond, regardless of a user’s location.</p> <p>For example, the UNIX timestamp <code dir="auto">1723472500000</code> always represents the time 1,723,472,500,000 milliseconds (1,723,472,500 seconds) after the UNIX Epoch. This timestamp’s meaning does <strong>not</strong> change relative to any <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>.</p> <p>To <em>format</em> an “int” UNIX timestamp into a readable date/time “string” expressed in a specific time zone, use the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function. The function does not <em>modify</em> UNIX timestamps. It simply <em>represents</em> timestamps in a desired human-readable format.</p> <p>For instance, the function can represent the UNIX timestamp <code dir="auto">1723472500000</code> as a “string” in several ways, depending on its <code dir="auto">format</code> and <code dir="auto">timezone</code> arguments, without changing the <em>absolute</em> point in time that it refers to. The simple script below calculates three valid representations of this timestamp and displays them in the <a href="/pine-script-docs/writing/debugging/#pine-logs">Pine Logs</a> pane:</p> <p><img src="/pine-script-docs/_astro/Time-Unix-timestamps-1.CTbXturQ_ZA7lsx.webp" alt="image" width="1040" height="316" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;UNIX timestamps demo&quot;) //@variable A UNIX time value representing the specific point 1,723,472,500,000 ms after the UNIX Epoch. int unixTimestamp = 1723472500000 // These are a few different ways to express the `unixTimestamp` in a relative, human-readable format. // Despite their format and time zone differences, all the calculated strings represent the SAME distinct point: string isoExchange = str.format_time(unixTimestamp) string utcDateTime = str.format_time(unixTimestamp, &quot;MM/dd/yyyy HH:mm:ss.S&quot;, &quot;UTC+0&quot;) string utc4TimeDate = str.format_time(unixTimestamp, &quot;hh:mm:ss a, MMMM dd, yyyy z&quot;, &quot;UTC+4&quot;) // Log the `unixTimestamp` and the custom &quot;string&quot; representations on the first bar. if barstate.isfirst log.info( &quot;\nUNIX time (ms): {0, number, #}\n ISO 8601 representation (Exchange time zone): {1}\n Custom date and time representation (UTC+0 time zone): {2}\n Custom time and date representation (UTC+4 time zone): {3}&quot;, unixTimestamp, isoExchange, utcDateTime, utc4TimeDate )</div> <p>Note that:</p> <ul> <li>The value enclosed within square brackets in the logged message is an <em>automatic</em> prefix representing the historical time of the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_log.info">log.info()</a> call in <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> format, expressed in the <a href="/pine-script-docs/concepts/time/#time-zones">chart time zone</a>.</li> </ul> <p>See the <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">Formatting dates and times</a> section to learn more about representing UNIX timestamps with formatted strings.</p> <h2 id="time-zones" class="md-heading"><a href="#time-zones">Time zones<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>A <a href="https://en.wikipedia.org/wiki/Time_zone" rel="nofollow">time zone</a> is a geographic region with an assigned <em>local time</em>. The specific time within a time zone is consistent throughout the region. Time zone boundaries typically relate to a location’s longitude. However, in practice, they tend to align with administrative boundaries rather than strictly following longitudinal lines.</p> <p>The local time within a time zone depends on its defined <em>offset</em> from <a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time" rel="nofollow">Coordinated Universal Time (UTC)</a>, which can range from UTC-12:00 (12 hours <em>behind</em> UTC) to UTC+14:00 (14 hours <em>ahead</em> of UTC). Some regions maintain a consistent offset from UTC, and others have an offset that changes over time due to <a href="https://en.wikipedia.org/wiki/Daylight_saving_time" rel="nofollow">daylight saving time (DST)</a> and other factors.</p> <p>Two primary time zones apply to data feeds and TradingView charts: the <em>exchange time zone</em> and the <em>chart time zone</em>.</p> <p>The exchange time zone represents the time zone of the current symbol’s <em>exchange</em>, which Pine scripts can access with the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> variable. <a href="/pine-script-docs/concepts/time/#calendar-based-variables">Calendar-based variables</a>, such as <a href="https://www.tradingview.com/pine-script-reference/v6/#var_month">month</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofweek">dayofweek</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_hour">hour</a>, always hold values expressed in the exchange time zone, and all time function overloads that have a <code dir="auto">timezone</code> parameter use this time zone by default.</p> <p>The chart time zone is a <em>visual preference</em> that defines how the chart and the time prefixes of <a href="/pine-script-docs/writing/debugging/#pine-logs">Pine Logs</a> represent time values. To set the chart time zone, use the “Timezone” input in the “Symbol” tab of the chart’s settings or click on the current time shown below the chart. The specified time zone does <strong>not</strong> affect time calculations in Pine scripts because they cannot access this chart information. Although scripts cannot access a chart’s time zone, programmers can provide <a href="/pine-script-docs/concepts/inputs/">inputs</a> that users can adjust to match the time zone.</p> <p>For example, the script below uses <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> to represent the <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> of the last historical bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">opening time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">closing time</a> as date-time strings, expressed in the function’s default time zone, the exchange time zone, UTC-0, and a user-specified time zone. It displays all four representations for comparison within a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> in the bottom-right corner of the chart:</p> <p><img src="/pine-script-docs/_astro/Time-Time-zones-1.BAv9QYBX_jNyY.webp" alt="image" width="1336" height="506" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Time zone comparison demo&quot;, overlay = true) //@variable The time zone of the time values in the last table row. // The &quot;string&quot; can contain either UTC offset notation or an IANA time zone identifier. string timezoneInput = input.string(&quot;UTC+4:00&quot;, &quot;Time zone&quot;) //@variable A `table` showing strings representing bar times in three preset time zones and a custom time zone. var table displayTable = table.new( position.bottom_right, columns = 3, rows = 5, border_color = chart.fg_color, border_width = 2 ) //@function Initializes three `displayTable` cells on the `row` that show the `title`, `text1`, and `text2` strings. tableRow(int row, string title, string text1, string text2, color titleColor = #9b27b066, color infoColor = na) =&gt; displayTable.cell(0, row, title, bgcolor = titleColor, text_color = chart.fg_color) displayTable.cell(1, row, text1, bgcolor = infoColor, text_color = chart.fg_color) displayTable.cell(2, row, text2, bgcolor = infoColor, text_color = chart.fg_color) if barstate.islastconfirmedhistory // Draw an empty label to signify the bar that the displayed time strings represent. label.new(bar_index, high, color = #9b27b066, size = size.huge) //@variable The formatting string for all `str.format_time()` calls. Sets the format of the date-time strings. var string formatString = &quot;yyyy-MM-dd HH:mm:ss&quot; // Initialize a header row at the top of the `displayTable`. tableRow(0, &quot;&quot;, &quot;OPEN time&quot;, &quot;CLOSE time&quot;, na, #9b27b066) // Initialize a row showing the bar&#39;s times in the default time zone (no specified `timezone` arguments). tableRow(1, &quot;Default&quot;, str.format_time(time, formatString), str.format_time(time_close, formatString)) // Initialize a row showing the bar&#39;s times in the exchange time zone (`syminfo.timezone`). tableRow(2, &quot;Exchange: &quot; + syminfo.timezone, str.format_time(time, formatString, syminfo.timezone), str.format_time(time_close, formatString, syminfo.timezone) ) // Initialize a row showing the bar&#39;s times in the UTC-0 time zone (using &quot;UTC&quot; as the `timezone` arguments). tableRow(3, &quot;UTC-0&quot;, str.format_time(time, formatString, &quot;UTC&quot;), str.format_time(time_close, formatString, &quot;UTC&quot;)) // Initialize a row showing the bar&#39;s times in the custom time zone (`timezoneInput`). tableRow( 4, &quot;Custom: &quot; + timezoneInput, str.format_time(time, formatString, timezoneInput), str.format_time(time_close, formatString, timezoneInput) )</div> <p>Note that:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> on the chart signifies which bar’s times the displayed strings represent.</li> <li>The “Default” and “Exchange” rows in the table show identical results because <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> is the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function’s default <code dir="auto">timezone</code> argument.</li> <li>The exchange time zone on our example chart appears as <code dir="auto">&quot;America/New_York&quot;</code>, the <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="nofollow">IANA identifier</a> for the NASDAQ exchange’s time zone. It represents UTC-4 <em>or</em> UTC-5, depending on the time of year. See the <a href="/pine-script-docs/concepts/time/#time-zone-strings">next section</a> to learn more about time zone strings.</li> </ul> <h3 id="time-zone-strings" class="md-heading"><a href="#time-zone-strings">Time zone strings<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>All built-in functions with a <code dir="auto">timezone</code> parameter accept a “string” argument specifying the <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a> they use in their calculations. These functions can accept time zone strings in either of the following formats:</p> <ul> <li><strong>UTC</strong> (or <em>GMT</em>) offset notation, e.g., <code dir="auto">&quot;UTC-5&quot;</code>, <code dir="auto">&quot;UTC+05:30&quot;</code>, <code dir="auto">&quot;GMT+0100&quot;</code></li> <li><strong>IANA database</strong> notation, e.g., <code dir="auto">&quot;America/New_York&quot;</code>, <code dir="auto">&quot;Asia/Calcutta&quot;</code>, <code dir="auto">&quot;Europe/Paris&quot;</code></li> </ul> <p>The <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="nofollow">IANA time zone database</a> reference page lists possible time zone identifiers and their respective UTC offsets. The listed identifiers are valid as <code dir="auto">timezone</code> arguments.</p> <p>Note that various time zone strings expressed in UTC or IANA notation can represent the <em>same</em> offset from Coordinated Universal Time. For instance, these strings all represent a local time three hours ahead of UTC:</p> <ul> <li><code dir="auto">&quot;UTC+3&quot;</code></li> <li><code dir="auto">&quot;GMT+03:00&quot;</code></li> <li><code dir="auto">&quot;Asia/Kuwait&quot;</code></li> <li><code dir="auto">&quot;Europe/Moscow&quot;</code></li> <li><code dir="auto">&quot;Africa/Nairobi&quot;</code></li> </ul> <p>For the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function and the functions that calculate calendar-based values from a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a>, including <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a>, the “string” passed to the <code dir="auto">timezone</code> parameter changes the returned value’s calculation to express the result in the specified time zone. See the <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">Formatting dates and times</a> and <a href="/pine-script-docs/concepts/time/#calendar-based-functions">Calendar-based functions</a> sections for more information.</p> <p>The example below shows how time zone strings affect the returned values of calendar-based functions. This script uses three <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a> function calls to calculate “int” values representing the opening hour of each bar in the exchange time zone, UTC-0, and a user-specified UTC offset. It plots all three calculated hours in a separate pane for comparison:</p> <p><img src="/pine-script-docs/_astro/Time-Time-zones-Time-zone-strings-1.CWMAwPYS_ZN7mN7.webp" alt="image" width="1336" height="570" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Time zone strings in calendar functions demo&quot;) //@variable An &quot;int&quot; representing the user-specified hourly offset from UTC. int utcOffsetInput = input.int(defval = 4, title =&quot;Timezone offset UTC (+/-)&quot;, minval = -12, maxval = 14) //@variable A valid time zone string based on the `utcOffsetInput`, in UTC offset notation (e.g., &quot;UTC-4&quot;). string customOffset = &quot;UTC&quot; + (utcOffsetInput &gt; 0 ? &quot;+&quot; : &quot;&quot;) + str.tostring(utcOffsetInput) //@variable The bar&#39;s opening hour in the exchange time zone (default). Equivalent to the `hour` variable. int exchangeHour = hour(time) //@variable The bar&#39;s opening hour in the &quot;UTC-0&quot; time zone. int utcHour = hour(time, &quot;UTC-0&quot;) //@variable The bar&#39;s opening hour in the `customOffset` time zone. int customOffsetHour = hour(time, customOffset) // Plot the `exchangeHour`, `utcHour`, and `customOffsetHour` for comparison. plot(exchangeHour, &quot;Exchange hour&quot;, #E100FF5B, 8) plot(utcHour, &quot;UTC-0 hour&quot;, color.blue, 3) plot(customOffsetHour, &quot;Custom offset hour&quot;, color.orange, 3)</div> <p>Note that:</p> <ul> <li>The <code dir="auto">exchangeHour</code> value is four <em>or</em> five hours behind the <code dir="auto">utcHour</code> because the NASDAQ exchange is in the “America/New_York” time zone. This time zone has a UTC offset that <em>changes</em> during the year due to daylight saving time (DST). The script’s default <code dir="auto">customOffsetHour</code> is consistently four hours ahead of the <code dir="auto">utcHour</code> because its time zone is UTC+4.</li> <li>The call to the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a> function without a specified <code dir="auto">timezone</code> argument returns the same value that the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_hour">hour</a> <em>variable</em> holds because both represent the bar’s opening hour in the exchange time zone (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a>).</li> </ul> <p>For functions that return <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> directly, such as <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a>, the <code dir="auto">timezone</code> parameter defines the time zone of the function’s calendar-based <em>parameters</em>, including <code dir="auto">session</code>, <code dir="auto">year</code>, <code dir="auto">month</code>, <code dir="auto">day</code>, <code dir="auto">hour</code>, <code dir="auto">minute</code>, and <code dir="auto">second</code>. The parameter does <em>not</em> determine the time zone of the returned value, as UNIX timestamps are <em>time zone-agnostic</em>. See the <a href="/pine-script-docs/concepts/time/#testing-for-sessions">Testing for sessions</a> and <a href="/pine-script-docs/concepts/time/#timestamp">`timestamp()`</a> sections to learn more.</p> <p>The following script calls the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function to calculate the UNIX timestamp of a specific date and time, and it draws a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> at the timestamp’s corresponding bar location. The user-selected <code dir="auto">timezone</code> argument (<code dir="auto">timezoneInput</code>) determines the time zone of the call’s calendar-based arguments. Consequently, the calculated timestamp varies with the <code dir="auto">timezoneInput</code> value because identical local times in various time zones correspond to <em>different</em> amounts of time elapsed since the <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX Epoch</a>:</p> <p><img src="/pine-script-docs/_astro/Time-Time-zones-Time-zone-strings-2.Bgcl5BRA_Z1UUd3e.webp" alt="image" width="1336" height="500" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Time zone strings in UNIX timestamp functions demo&quot;, overlay = true) //@variable The `timezone` argument of the `timestamp()` call, which sets the time zone of all date and time parameters. string timezoneInput = input.string(&quot;Etc/UTC&quot;, &quot;Time zone&quot;) //@variable The UNIX timestamp corresponding to a specific calendar date and time. // The specified `year`, `month`, `day`, `hour`, `minute`, and `second` represent calendar values in the // `timezoneInput` time zone. // Different `timezone` arguments produce different UNIX timestamps because an identical date in another // time zone does NOT represent the same absolute point in time. int unixTimestamp = timestamp( timezone = timezoneInput, year = 2024, month = 10, day = 31, hour = 0, minute = 0, second = 0 ) //@variable The `close` value when the bar&#39;s opening time crosses the `unixTimestamp`. float labelPrice = ta.valuewhen(ta.cross(time, unixTimestamp), close, 0) // On the last historical bar, draw a label showing the `unixTimestamp` value at the corresponding bar location. if barstate.islastconfirmedhistory label.new( unixTimestamp, nz(labelPrice, close), &quot;UNIX timestamp: &quot; + str.tostring(unixTimestamp), xloc.bar_time, yloc.price, chart.fg_color, label.style_label_down, chart.bg_color, size.large )</div> <p>Note that:</p> <ul> <li><code dir="auto">&quot;Etc/UTC&quot;</code> is the <em>IANA identifier</em> for the UTC+0 time zone.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_label.new">label.new()</a> call uses <a href="https://www.tradingview.com/pine-script-reference/v6/#const_xloc.bar_time">xloc.bar_time</a> as its <code dir="auto">xloc</code> argument, which is required to anchor the drawing to an absolute time value. Without this argument, the function treats the <code dir="auto">unixTimestamp</code> as a relative bar index, leading to an incorrect location.</li> <li>The label’s <code dir="auto">y</code> value is the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_close">close</a> of the bar where the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">opening time</a> crosses the <code dir="auto">unixTimestamp</code>. If the timestamp represents a future time, the label uses the last historical bar’s price.</li> </ul> <p>Although time zone strings can use either UTC or IANA notation, we recommend using <em>IANA notation</em> for <code dir="auto">timezone</code> arguments in most cases, especially if a script’s time calculations must align with the observed time offset in a specific country or subdivision. When a time function call uses an <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="nofollow">IANA time zone identifier</a> for its <code dir="auto">timezone</code> argument, its calculations adjust automatically for historical and future changes to the specified region’s observed time, such as daylight saving time (DST) and updates to time zone boundaries, instead of using a fixed offset from UTC.</p> <p>The following script demonstrates how UTC and IANA time zone strings can affect time calculations differently. It uses two calls to the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a> function to calculate the hour from the current bar’s opening timestamp using <code dir="auto">&quot;UTC-4&quot;</code> and <code dir="auto">&quot;America/New_York&quot;</code> as <code dir="auto">timezone</code> arguments. The script plots the results of both calls for comparison and colors the main pane’s background when the returned values do not match. Although these two <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a> calls may seem similar because UTC-4 is an observed UTC offset in New York, they <em>do not</em> always return the same results, as shown below:</p> <p><img src="/pine-script-docs/_astro/Time-Time-zones-Time-zone-strings-3.dJwwxe9V_1a20aC.webp" alt="image" width="1422" height="602" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;UTC vs IANA time zone strings demo&quot;) //@variable The hour of the current `time` in the &quot;UTC-4&quot; time zone. // This variable&#39;s value represents the hour in New York only during DST. It is one hour ahead otherwise. int hourUTC = hour(time, &quot;UTC-4&quot;) //@variable The hour of the current `time` in the &quot;America/New_York&quot; time zone. // This form adjusts to UTC offset changes automatically, so the value always represents the hour in New York. int hourIANA = hour(time, &quot;America/New_York&quot;) //@variable Is translucent blue when `hourUTC` does not equal `hourIANA`, `na` otherwise. color bgColor = hourUTC != hourIANA ? color.rgb(33, 149, 243, 80) : na // Plot the values of `hourUTC` and `hourIANA` for comparison. plot(hourUTC, &quot;UTC-4&quot;, color.blue, linewidth = 6) plot(hourIANA, &quot;America/New_York&quot;, color.orange, linewidth = 3) // Highlight the main chart pane with the `bgColor`. bgcolor(bgColor, title = &quot;Unequal result highlight&quot;, force_overlay = true)</div> <p>The plots in the chart above diverge periodically because New York observes daylight saving time, meaning its UTC offset <em>changes</em> at specific points in a year. During DST, New York’s local time follows UTC-4. Otherwise, it follows UTC-5. Because the script’s first <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a> call uses <code dir="auto">&quot;UTC-4&quot;</code> as its <code dir="auto">timezone</code> argument, it returns the correct hour in New York <em>only</em> during DST. In contrast, the call that uses the <code dir="auto">&quot;America/New_York&quot;</code> time zone string adjusts its UTC offset automatically to return the correct hour in New York at <em>any</em> time of the year.</p> <h2 id="time-variables" class="md-heading"><a href="#time-variables">Time variables<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Pine Script™ has several built-in variables that provide scripts access to different forms of time information:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> variables hold <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> representing the current bar’s opening and closing times, respectively.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> variable holds a UNIX timestamp representing the starting time of the last UTC calendar day in a session.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> variable holds a UNIX timestamp representing the current time when the script executes.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_year">year</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_month">month</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_weekofyear">weekofyear</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofmonth">dayofmonth</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofweek">dayofweek</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_hour">hour</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_minute">minute</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_second">second</a> variables reference calendar values based on the current bar’s opening time, expressed in the <a href="/pine-script-docs/concepts/time/#time-zones">exchange time zone</a>.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> variable holds a UNIX timestamp representing the last available bar’s opening time.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.left_visible_bar_time">chart.left_visible_bar_time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.right_visible_bar_time">chart.right_visible_bar_time</a> variables hold UNIX timestamps representing the opening times of the leftmost and rightmost visible chart bars.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> variable holds a “string” value representing the <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a> of the current symbol’s exchange in <a href="/pine-script-docs/concepts/time/#time-zone-strings">IANA database notation</a>. All time-related function overloads with a <code dir="auto">timezone</code> parameter use this variable as the default argument.</li> </ul> <h3 id="time-and-time_close-variables" class="md-heading"><a href="#time-and-time_close-variables">`time` and `time_close` variables<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> variable holds the <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> of the current bar’s <em>opening time</em>, and the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> variable holds the UNIX timestamp of the bar’s <em>closing time</em>.</p> <p>These timestamps are unique, time zone-agnostic “int” values, which programmers can use to anchor <a href="/pine-script-docs/language/type-system/#drawing-types">drawing objects</a> to specific bar times, calculate and inspect bar time differences, construct readable date/time strings with the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function, and more.</p> <p>The script below displays bar opening and closing times in different ways. On each bar, it <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formats</a> the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> timestamps into strings containing the hour, minute, and second in the <a href="/pine-script-docs/concepts/time/#time-zones">exchange time zone</a>, and it draws <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> displaying the formatted strings at the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_open">open</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_close">close</a> prices. Additionally, the script displays strings containing the unformatted UNIX timestamps of the last chart bar within a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> in the bottom-right corner:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Time-and-time-close-1.SQRlfJYt_Z2oHBgb.webp" alt="image" width="1280" height="576" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`time` and `time_close` demo&quot;, overlay = true, max_labels_count = 500) //@variable A &quot;string&quot; representing the hour, minute, and second of the bar&#39;s opening time in the exchange time zone. string openTimeString = str.format_time(time, &quot;HH:mm:ss&quot;) //@variable A &quot;string&quot; representing the hour, minute, and second of the bar&#39;s closing time in the exchange time zone. string closeTimeString = str.format_time(time_close, &quot;HH:mm:ss&quot;) //@variable Is `label.style_label_down` when the `open` is higher than `close`, `label.style_label_up` otherwise. string openLabelStyle = open &gt; close ? label.style_label_down : label.style_label_up //@variable Is `label.style_label_down` when the `close` is higher than `open`, `label.style_label_up` otherwise. string closeLabelStyle = close &gt; open ? label.style_label_down : label.style_label_up // Draw labels anchored to the bar&#39;s `time` to display the `openTimeString` and `closeTimeString`. label.new(time, open, openTimeString, xloc.bar_time, yloc.price, color.orange, openLabelStyle, color.white) label.new(time, close, closeTimeString, xloc.bar_time, yloc.price, color.blue, closeLabelStyle, color.white) if barstate.islast //@variable A `table` displaying the last bar&#39;s *unformatted* UNIX timestamps. var table t = table.new(position.bottom_right, 2, 2, bgcolor = #ffe70d) // Populate the `t` table with &quot;string&quot; representations of the the &quot;int&quot; `time` and `time_close` values. t.cell(0, 0, &quot;`time`&quot;) t.cell(1, 0, str.tostring(time)) t.cell(0, 1, &quot;`time_close`&quot;) t.cell(1, 1, str.tostring(time_close))</div> <p>Note that:</p> <ul> <li>This script’s <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_label.new">label.new()</a> calls include <a href="https://www.tradingview.com/pine-script-reference/v6/#const_xloc.bar_time">xloc.bar_time</a> as the <code dir="auto">xloc</code> argument and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> as the <code dir="auto">x</code> argument to anchor the drawings to bar opening times.</li> <li>The formatted strings express time in the exchange time zone because we did not specify <code dir="auto">timezone</code> arguments in the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> calls. NYSE, our chart symbol’s exchange, is in the “America/New_York” time zone (UTC-4/-5).</li> <li>Although our example chart uses an <em>hourly</em> timeframe, the <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> and the <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> at the end of the chart show that the last bar closes only <em>30 minutes</em> (1,800,000 milliseconds) after opening. This behavior occurs because the chart aligns bars with <a href="/pine-script-docs/concepts/sessions/">session</a> opening and closing times. A session’s final bar closes when the session ends, and a new bar opens when a new session starts. A typical session on our 60-minute chart with regular trading hours (RTH) spans from 09:30 to 16:00 (6.5 hours). The chart divides this interval into as many 60-minute bars as possible, starting from the session’s opening time, which leaves only 30 minutes for the final bar to cover.</li> </ul> <p>It’s crucial to note that unlike the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> variable, which has consistent behavior across chart types, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> behaves differently on <em>time-based</em> and <em>non-time-based</em> charts.</p> <p>Time-based charts have bars that typically open and close at regular, <em>predictable</em> times within a session. Thanks to this predictability, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> can accurately represent the <em>expected</em> closing time of an open bar on a time-based chart, as shown on the last bar in the example above.</p> <p>In contrast, the bars on <a href="https://www.tradingview.com/support/solutions/43000709225/">tick charts</a> and <em>price-based</em> charts (all <a href="/pine-script-docs/concepts/non-standard-charts-data/">non-standard charts</a> excluding <a href="https://www.tradingview.com/support/solutions/43000619436/">Heikin Ashi</a>) cover <em>irregular</em> time intervals. Tick charts construct bars based on successive ticks in the data feed, and price-based charts construct bars based on significant price movements. The time it takes for new ticks or price changes to occur is <em>unpredictable</em>. As such, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> value is <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> on the <em>realtime bars</em> of these charts.</p> <p>The following script uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> variables with <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.tostring">str.tostring()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> to create strings containing bar opening and closing <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> and <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> date-time representations, which it displays in <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> at each bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_high">high</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_low">low</a> prices.</p> <p>When applied to a <a href="https://www.tradingview.com/support/solutions/43000502284/">Renko</a> chart, which forms new bars based on <em>price movements</em>, the labels show correct results on all historical bars. However, the last bar has a <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> value of <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> because the future closing time is unpredictable. Consequently, the bar’s closing time label shows a timestamp of <code dir="auto">&quot;NaN&quot;</code> and an <em>incorrect</em> date and time:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Time-and-time-close-2.DgbsHPgj_1bCOHq.webp" alt="image" width="1336" height="610" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`time_close` on non-time-based chart demo&quot;, overlay = true) //@variable A formatted &quot;string&quot; containing the date and time that `time_close` represents in the exchange time zone. string formattedCloseTime = str.format_time(time_close, format = &quot;&#39;Date and time:&#39; yyyy-MM-dd HH:mm:ss&quot;) //@variable A formatted &quot;string&quot; containing the date and time that `time` represents in the exchange time zone. string formattedOpenTime = str.format_time(time, format = &quot;&#39;Date and time:&#39; yyyy-MM-dd HH:mm:ss&quot;) //@variable A &quot;string&quot; containing the `time_close` UNIX timestamp and the `formattedCloseTime`. string closeTimeText = str.format(&quot;Close timestamp: {0,number,#}\n{1}&quot;, time_close, formattedCloseTime) //@variable A &quot;string&quot; containing the `time` UNIX timestamp and the `formattedOpenTime`. string openTimeText = str.format(&quot;Open timestamp: {0,number,#}\n{1}&quot;, time, formattedOpenTime) // Define label colors for historical and realtime bars. color closeLabelColor = barstate.islast ? color.purple : color.aqua color openLabelColor = barstate.islast ? color.green : color.orange // Draw a label at the `high` to display the `closeTimeText` and a label at the `low` to display the `openTimeText`, // both anchored to the bar&#39;s `time`. label.new( time, high, closeTimeText, xloc.bar_time, color = closeLabelColor, textcolor = color.white, size = size.large, textalign = text.align_left ) label.new( time, low, openTimeText, xloc.bar_time, color = openLabelColor, style = label.style_label_up, size = size.large, textcolor = color.white, textalign = text.align_left ) // Highlight the background yellow on the latest bar. bgcolor(barstate.islast ? #f3de22cb : na, title = &quot;Latest bar highlight&quot;)</div> <p>Note that:</p> <ul> <li>The script draws up to 50 <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> because we did not specify a <code dir="auto">max_labels_count</code> argument in the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_indicator">indicator()</a> declaration statement.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function replaces <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> values with 0 in its calculations, which is why it returns an incorrect date-time “string” on the last bar. A timestamp of 0 corresponds to the <em>UNIX Epoch</em> (00:00:00 UTC on January 1, 1970). However, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> call does not specify a <code dir="auto">timezone</code> argument, so it expresses the epoch’s date and time in the <em>exchange time zone</em>, which was five hours behind UTC at that point in time.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> function, which returns the closing timestamp of a bar on a specified timeframe within a given session, also returns <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> on the realtime bars of tick-based and price-based charts.</li> </ul> <h3 id="time_tradingday" class="md-heading"><a href="#time_tradingday">`time_tradingday`<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> variable holds a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> representing the starting time (00:00 UTC) of the last trading day in the current bar’s final session. It is helpful primarily for date and time calculations on <em>time-based</em> charts for symbols with overnight sessions that start and end on <em>different</em> calendar days.</p> <p>On “1D” and lower timeframes, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> timestamp corresponds to the beginning of the day when the current session <em>ends</em>, even for bars that open and close on the previous day. For example, the “Monday” session for “EURUSD” starts on Sunday at 17:00 and ends on Monday at 17:00 in the <a href="/pine-script-docs/concepts/time/#time-zones">exchange time zone</a>. The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> values of <em>all</em> intraday bars within the session represent Monday at 00:00 UTC.</p> <p>On timeframes higher than “1D”, which can cover <em>multiple</em> sessions, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> holds the timestamp representing the beginning of the last calendar day of the bar’s <em>final</em> trading session. For example, on a “EURUSD, 1W” chart, the timestamp represents the start of the last trading day in the week, which is typically Friday at 00:00 UTC.</p> <p>The script below demonstrates how the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> variables differ on Forex symbols. On each bar, it draws <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> to display strings containing the variables’ <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> and <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted dates and times</a>. It also uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> function to calculate the UTC calendar day from both timestamps, highlighting the background when the calculated days do not match.</p> <p>When applied to the “FXCM:EURUSD” chart with the “3h” (“180”) timeframe, the script highlights the background of the <em>first bar</em> in each session, as each session opens on the <em>previous</em> calendar day. The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> call that uses <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> calculates the opening day on the session’s first bar, whereas the call that uses <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> calculates the day when the session <em>ends</em>:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Time-tradingday-1.oi6pPbd1_Z2d200U.webp" alt="image" width="1336" height="612" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`time_tradingday` demo&quot;, overlay = true) //@variable A concatenated &quot;string&quot; containing the `time_tradingday` timestamp and a formatted representation in UTC. string tradingDayText = &quot;`time_tradingday`: &quot; + str.tostring(time_tradingday) + &quot;\n&quot; + &quot;Date and time: &quot; + str.format_time(time_tradingday, &quot;dd MMM yyyy, HH:mm (z)&quot;, &quot;UTC+0&quot;) //@variable A concatenated &quot;string&quot; containing the `time` timestamp and a formatted representation in UTC. string barOpenText = &quot;`time`: &quot; + str.tostring(time) + &quot;\n&quot; + &quot;Date and time: &quot; + str.format_time(time, &quot;dd MMM yyyy, HH:mm (z)&quot;, &quot;UTC+0&quot;) //@variable Is `true` on every even bar, `false` otherwise. This condition determines the appearance of the labels. bool isEven = bar_index % 2 == 0 // The `yloc` and `style` properties of the labels. They alternate on every other bar for visibility. labelYloc = isEven ? yloc.abovebar : yloc.belowbar labelStyle = isEven ? label.style_label_down : label.style_label_up // Draw alternating labels anchored to the bar&#39;s `time` to display the `tradingDayText` and `barOpenText`. if isEven label.new(time, 0, tradingDayText + &quot;\n\n\n&quot;, xloc.bar_time, labelYloc, color.teal, labelStyle, color.white) label.new(time, 0, barOpenText, xloc.bar_time, labelYloc, color.maroon, labelStyle, color.white) else label.new(time, 0, &quot;\n\n\n&quot; + barOpenText, xloc.bar_time, labelYloc, color.maroon, labelStyle, color.white) label.new(time, 0, tradingDayText, xloc.bar_time, labelYloc, color.teal, labelStyle, color.white) //@variable The day of the month, in UTC, that the `time_tradingday` timestamp corresponds to. int tradingDayOfMonth = dayofmonth(time_tradingday, &quot;UTC+0&quot;) //@variable The day of the month, in UTC, that the `time` timestamp corresponds to. int openingDayOfMonth = dayofmonth(time, &quot;UTC+0&quot;) // Highlight the background when the `tradingDayOfMonth` does not equal the `openingDayOfMonth`. bgcolor(tradingDayOfMonth != openingDayOfMonth ? color.rgb(174, 89, 243, 85) : na, title = &quot;Different day highlight&quot;)</div> <p>Note that:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> calls use <code dir="auto">&quot;UTC+0&quot;</code> as the <code dir="auto">timezone</code> argument, meaning the results represent calendar time values with no offset from UTC. In the screenshot, the first bar opens at 21:00 UTC, 17:00 in the exchange time zone (“America/New_York”).</li> <li>The formatted strings show <code dir="auto">&quot;GMT&quot;</code> as the acronym of the time zone, which is equivalent to <code dir="auto">&quot;UTC+0&quot;</code> in this context.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_tradingday">time_tradingday</a> value is the same for <em>all</em> three-hour bars within each session, even for the initial bar that opens on the previous UTC calendar day. The assigned timestamp changes only when a new session starts.</li> </ul> <h3 id="timenow" class="md-heading"><a href="#timenow">`timenow`<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> variable holds a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> representing the script’s <em>current time</em>. Unlike the values of other variables that hold UNIX timestamps, the values in the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> series correspond to times when the script <em>executes</em>, not the times of specific bars or trading days.</p> <p>A Pine script executes only <em>once</em> per historical bar, and all historical executions occur when the script first <em>loads</em> on the chart. As such, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> value is relatively consistent on historical bars, with only occasional millisecond changes across the series. In contrast, on realtime bars, a script executes once for <em>each new update</em> in the data feed, which can happen several times per bar. With each new execution, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> value updates on the latest bar to represent the current time.</p> <aside aria-label="Notice!" class="tv-informer not-content" style="--informer-header-color:#FF9800;--informer-back-color-light:#FFF3E0;--informer-back-color-dark:#33261A"><div class="tv-informer-icon"><svg viewBox="0 0 18 18" width="18" height="18"><path fill-rule="evenodd" d="M9 17A8 8 0 1 0 9 1a8 8 0 0 0 0 16ZM9 4c-.79 0-1.38.7-1.25 1.48l.67 4.03a.59.59 0 0 0 1.16 0l.67-4.03A1.27 1.27 0 0 0 9 4Zm0 8a1 1 0 1 0 0 2 1 1 0 0 0 0-2Z" fill="currentColor"></path></svg></div><div class="tv-informer-content"><p><span class="tv-informer-header">Notice!</span>Because <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> only updates after script executions, its value does <strong>not</strong> always correspond to the <em>continuous</em> time displayed below the chart. When no new updates are available in the realtime data feed, a script on the chart remains <em>idle</em>, in which case the variable’s timestamp does not change.</p></div></aside> <p>This variable is most useful on realtime bars, where programmers can apply it to track the times of the latest script executions, count the time elapsed within open bars, control drawings based on bar updates, and more.</p> <p>The script below inspects the value of <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> on the latest chart bars and uses it to analyze realtime bar updates. When the script first reaches the last chart bar, it declares three variables with the <a href="https://www.tradingview.com/pine-script-reference/v6/#kw_varip">varip</a> keyword to hold the latest <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> value, the total time elapsed between the bar’s updates, and the total number of updates. It uses these values to calculate the average number of milliseconds between updates, which it displays in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> along with the current execution’s timestamp, a <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> time and date in the <a href="/pine-script-docs/concepts/time/#time-zones">exchange time zone</a>, and the current number of bar updates:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Timenow-1.OYWKIkvY_Z1MUwEq.webp" alt="image" width="1336" height="678" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`timenow` demo&quot;, overlay = true, max_labels_count = 500) if barstate.islast //@variable Holds the UNIX timestamp of the latest script execution for timing updates in the data feed. varip int lastUpdateTimestamp = timenow //@variable The total number of milliseconds elapsed across the bar&#39;s data updates. varip int totalUpdateTime = 0 //@variable The number of updates that have occurred on the current bar. varip int numUpdates = 0 // Add the time elapsed from the `lastUpdateTimestamp` to `totalUpdateTime`, increase the `numUpdates` counter, and // update the `lastUpdateTimestamp` when the `timenow` value changes. if timenow != lastUpdateTimestamp totalUpdateTime += timenow - lastUpdateTimestamp numUpdates += 1 lastUpdateTimestamp := timenow //@variable The average number of milliseconds elapsed between the bar&#39;s updates. float avgUpdateTime = nz(totalUpdateTime / numUpdates) //@variable Contains the `timenow` value, a custom representation, and the `numUpdates` and `avgUpdateTime` values. string displayText = &quot;`timenow`: &quot; + str.tostring(timenow) + &quot;\nTime and date (exchange): &quot; + str.format_time(timenow, &quot;HH:mm:ss MM/dd/yy&quot;) + &quot;\nNumber of updates: &quot; + str.tostring(numUpdates) + &quot;\nAvg. time between updates: &quot; + str.tostring(avgUpdateTime, &quot;#.###&quot;) + &quot; ms&quot; //@variable The color of the label. Is blue when the bar is open, and gray after it closes. color labelColor = barstate.isconfirmed ? color.gray : color.blue //@variable The label&#39;s y-coordinate. Alternates between `high` and `low` on every other bar. float labelPrice = bar_index % 2 == 0 ? high : low //@variable The label&#39;s style. Alternates between &quot;lower-right&quot; and &quot;upper-right&quot; styles. labelStyle = bar_index % 2 == 0 ? label.style_label_lower_right : label.style_label_upper_right // Draw a `labelColor` label anchored to the bar&#39;s `time` to show the `displayText`. label.new( time, labelPrice, displayText, xloc.bar_time, color = labelColor, style = labelStyle, textcolor = color.white, size = size.large ) // Reset the `totalUpdateTime` and `numUpdates` counters when the bar is confirmed (closes). if barstate.isconfirmed totalUpdateTime := 0 numUpdates := 0</div> <p>Note that:</p> <ul> <li>When a bar is unconfirmed (open), the <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> is blue to signify that additional updates can occur. After the bar is <a href="https://www.tradingview.com/pine-script-reference/v6/#var_barstate.isconfirmed">confirmed</a> (closed), the label turns gray.</li> <li>Although we’ve set the chart <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a> to match the exchange time zone, the formatted time in the open bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> and the time shown below the chart <em>do not</em> always align. The script records a new timestamp <em>only</em> when a new execution occurs, whereas the time below the chart updates <em>continuously</em>.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#kw_varip">varip</a> keyword specifies that a variable does not revert to the last committed value in its series when new updates occur. This behavior allows the script to use variables to track changes in <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> on an open bar.</li> <li>Updates to <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> on open realtime bars do not affect the recorded timestamps on confirmed bars as the script executes. However, the historical series changes (<em>repaints</em>) after reloading the chart because <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> references the script’s <em>current time</em>, not the times of specific bars.</li> </ul> <h3 id="calendar-based-variables" class="md-heading"><a href="#calendar-based-variables">Calendar-based variables<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_year">year</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_month">month</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_weekofyear">weekofyear</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofmonth">dayofmonth</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofweek">dayofweek</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_hour">hour</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_minute">minute</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_second">second</a> variables hold <em>calendar-based</em> “int” values calculated from the current bar’s <em>opening time</em>, expressed in the <a href="/pine-script-docs/concepts/time/#time-zones">exchange time zone</a>. These variables reference the same values that <a href="/pine-script-docs/concepts/time/#calendar-based-functions">calendar-based functions</a> return when they use the default <code dir="auto">timezone</code> argument and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> as the <code dir="auto">time</code> argument. For instance, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_year">year</a> variable holds the same value that a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_year">year(time)</a> call returns.</p> <p>Programmers can use these calendar-based variables for several purposes, such as:</p> <ul> <li>Identifying a bar’s opening date and time.</li> <li>Passing the variables to the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function to calculate <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a>.</li> <li>Testing when date/time values or ranges occur in a data feed.</li> </ul> <p>One of the most common use cases for these variables is checking for date or time ranges to control when a script displays visuals or executes calculations. This simple example inspects the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_year">year</a> variable to determine when to plot a visible value. If the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_year">year</a> is 2022 or higher, the script plots the bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_close">close</a>. Otherwise, it plots <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a>:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Calendar-based-variables-1.kYa8Z75h_KDMyu.webp" alt="image" width="1376" height="550" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`year` demo&quot;, overlay = true) // Plot the `close` price on bars that open in the `year` 2022 onward. Otherwise, plot `na` to display nothing. plot(year &gt;= 2022 ? close : na, &quot;`close` price (year 2022 and later)&quot;, linewidth = 3)</div> <p>When using these variables in conditions that isolate specific dates or times rather than ranges, it’s crucial to consider that certain conditions might not detect some occurrences of the values due to a chart’s timeframe, the opening times of chart bars, or the symbol’s active session.</p> <p>For instance, suppose we want to detect when the first calendar day of each month occurs on the chart. Intuitively, one might consider simply checking when the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofmonth">dayofmonth</a> value equals 1. However, this condition only identifies when a bar <em>opens</em> on a month’s first day. The bars on some charts can open and close in <em>different</em> months. Additionally, a chart bar might not contain the first day of a month if the market is <em>closed</em> on that day. Therefore, we must create extra conditions that work in these scenarios to identify the first day in <em>any</em> month on the chart.</p> <p>The script below uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofmonth">dayofmonth</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_month">month</a> variables, and the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a> function, to create three conditions that detect the first day of the month in different ways. The first condition detects if the bar opens on the first day, the second checks if the bar opens in one month and closes in another, and the third checks if the chart skips the date entirely. The script draws <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> showing bar opening dates and highlights the background with different colors to visualize when each condition occurs:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Calendar-based-variables-2.xQSXmhyL_Zw0zRE.webp" alt="image" width="1336" height="636" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Detecting the first day of the month demo&quot;, overlay = true, max_labels_count = 500) //@variable Is `true` only if the current bar opens on the first day of the month in exchange time, `false` otherwise. bool opensOnFirst = dayofmonth == 1 //@variable Is `true` if the bar opens in one month and closes in another, meaning its time span includes the first day. bool containsFirst = month != month(time_close) //@variable Is `true` only if the bar opens in a new month and the current or previous bar does not cover the first day. bool skipsFirst = month != month[1] and not (opensOnFirst or containsFirst[1]) //@variable The name of the current bar&#39;s opening weekday. string weekdayName = switch dayofweek dayofweek.sunday =&gt; &quot;Sunday&quot; dayofweek.monday =&gt; &quot;Monday&quot; dayofweek.tuesday =&gt; &quot;Tuesday&quot; dayofweek.wednesday =&gt; &quot;Wednesday&quot; dayofweek.thursday =&gt; &quot;Thursday&quot; dayofweek.friday =&gt; &quot;Friday&quot; dayofweek.saturday =&gt; &quot;Saturday&quot; //@variable A custom &quot;string&quot; representing the bar&#39;s opening date, including the weekday name. string openDateText = weekdayName + str.format_time(time, &quot;, MMM d, yyyy&quot;) // Draw a green label when the bar opens on the first day of the month. if opensOnFirst string labelText = &quot;Bar opened on\n&quot; + openDateText label.new(time, open, labelText, xloc.bar_time, color = color.green, textcolor = color.white, size = size.large) // Draw a blue label when the bar opens and closes in different months. if containsFirst string labelText = &quot;Bar includes the first day,\nbut opened on\n&quot; + openDateText label.new(time, open, labelText, xloc.bar_time, color = color.blue, textcolor = color.white, size = size.large) // Draw a red label when the chart skips the first day of the month. if skipsFirst string labelText = &quot;Chart doesn&#39;t include the first day.\nBar opened on\n&quot; + openDateText label.new(time, open, labelText, xloc.bar_time, color = color.red, textcolor = color.white, size = size.large) // Highlight the background when the conditions occur. bgcolor(opensOnFirst ? color.new(color.green, 70) : na, title = &quot;`opensOnFirst` condition highlight&quot;) bgcolor(containsFirst ? color.new(color.blue, 70) : na, title = &quot;`containsFirst` condition highlight&quot;) bgcolor(skipsFirst ? color.new(color.red, 70) : na, title = &quot;`skipsFirst` condition highlight&quot;)</div> <p>Note that:</p> <ul> <li>The script calls the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a> function with <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> as the <code dir="auto">time</code> argument to calculate each bar’s <em>closing</em> month for the <code dir="auto">containsFirst</code> condition.</li> <li>The <code dir="auto">dayofweek.*</code> namespace contains variables that hold each possible <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofweek">dayofweek</a> value, e.g., <a href="https://www.tradingview.com/pine-script-reference/v6/#const_dayofweek.sunday">dayofweek.sunday</a> has a constant value of 1 and <a href="https://www.tradingview.com/pine-script-reference/v6/#const_dayofweek.saturday">dayofweek.saturday</a> has a constant value of 7. The script compares <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofweek">dayofweek</a> to these variables in a <a href="https://www.tradingview.com/pine-script-reference/v6/#kw_switch">switch</a> structure to determine the weekday name shown inside each <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a>.</li> <li>To detect the <em>first opening time</em> in a monthly timeframe, not strictly the first day in a calendar month, use <code dir="auto">ta.change(time(&quot;1M&quot;)) &gt; 0</code> or <code dir="auto">timeframe.change(&quot;1M&quot;)</code> instead of conditions based on these variables. See the <a href="/pine-script-docs/concepts/time/#testing-for-changes-in-higher-timeframes">Testing for changes in higher timeframes</a> section to learn more.</li> </ul> <h3 id="last_bar_time" class="md-heading"><a href="#last_bar_time">`last_bar_time`<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> variable holds a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> representing the <em>last</em> available bar’s opening time. It is similar to <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_index">last_bar_index</a>, which references the latest bar index. On historical bars, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> consistently references the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value of the last bar available when the script first <em>loads</em> on the chart. The only time the variable’s value updates across script executions is when a new realtime bar opens.</p> <p>The following script uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> variable to get the opening timestamp of the last chart bar during its execution on the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_barstate.isfirst">first bar</a>. It displays the UNIX timestamp and a <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> date and time in a single-cell <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> created only on that bar. When the script executes on the last available bar, it draws a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> showing the bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value and its formatted representation for visual comparison.</p> <p>As the chart below shows, both drawings display <em>identical</em> times, verifying that <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> correctly references the last bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value on previous historical bars:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Last-bar-time-1.COWu3Po-_Z26jRdL.webp" alt="image" width="1336" height="540" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`last_bar_time` demo&quot;, overlay = true) if barstate.isfirst //@variable A single-cell `table`, created only on the *first* bar, showing the *last* available bar&#39;s opening time. table displayTable = table.new(position.bottom_right, 1, 1, color.aqua) //@variable A &quot;string&quot; containing the `last_bar_time` UNIX timestamp and a custom date and time representation. string lastBarTimeText = &quot;`last_bar_time`: &quot; + str.tostring(last_bar_time) + &quot;\nDate and time (exchange): &quot; + str.format_time(last_bar_time, &quot;dd/MM/yy HH:mm:ss&quot;) // Initialize the `displayTable` cell with the `lastBarTimeText`. displayTable.cell( 0, 0, lastBarTimeText, text_color = color.white, text_size = size.large, text_halign = text.align_left ) //@variable Is `true` only on the first occurrence of `barstate.islast`, `false` otherwise. // This condition occurs on the bar whose `time` the `last_bar_time` variable refers to on historical bars. bool isInitialLastBar = barstate.islast and not barstate.islast[1] if isInitialLastBar //@variable A &quot;string&quot; containing the last available bar&#39;s `time` value and a custom date and time representation. // Matches the `lastBarTimeText` from the first bar because `last_bar_time` equals this bar&#39;s `time`. string openTimeText = &quot;`time`: &quot; + str.tostring(time) + &quot;\nDate and time (exchange): &quot; + str.format_time(time, &quot;dd/MM/yy HH:mm:ss&quot;) // Draw a label anchored to the bar&#39;s `time` to display the `openTimeText`. label.new( time, high, openTimeText, xloc.bar_time, color = color.purple, textcolor = color.white, size = size.large, textalign = text.align_left ) // Highlight the background when `isInitialLastBar` is `true` for visual reference. bgcolor(barstate.islast ? color.rgb(155, 39, 176, 80) : na, title = &quot;Initial last bar highlight&quot;)</div> <p>Note that:</p> <ul> <li>The script creates the <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a> only on the <em>first</em> bar with the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_barstate.islast">barstate.islast</a> state because that bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value is what <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> equals on all historical bars. On subsequent bars, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> value <em>updates</em> to represent the latest realtime bar’s opening time.</li> <li>Updates to <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> on realtime bars do not affect the values on historical bars as the script executes. However, the variable’s series <em>repaints</em> when the script restarts because <a href="https://www.tradingview.com/pine-script-reference/v6/#var_last_bar_time">last_bar_time</a> always references the latest available bar’s opening time.</li> <li>This script expresses dates using the <code dir="auto">&quot;dd/MM/yy&quot;</code> format, meaning the two-digit day appears before the two-digit month, and the month appears before the two-digit representation of the year. See <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">this section</a> below for more information.</li> </ul> <h3 id="visible-bar-times" class="md-heading"><a href="#visible-bar-times">Visible bar times<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.left_visible_bar_time">chart.left_visible_bar_time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.right_visible_bar_time">chart.right_visible_bar_time</a> variables reference the opening <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> of the chart’s leftmost (first) and rightmost (last) <em>visible bars</em> on every script execution. When a script uses these variables, it responds dynamically to visible chart changes, such as users scrolling across bars or zooming in/out. Each time the visible window changes, the script <em>re-executes</em> automatically to update the variables’ values on all available bars.</p> <p>The example below demonstrates how the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.left_visible_bar_time">chart.left_visible_bar_time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.right_visible_bar_time">chart.right_visible_bar_time</a> variables work across script executions. The script draws <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> anchored to the visible bars’ times to display the UNIX timestamps. In addition, it draws two single-cell <a href="/pine-script-docs/concepts/tables/">tables</a> showing corresponding dates and times in the standard <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> format. The script creates these drawings only when it executes on the first bar. As the script continues to execute on subsequent bars, it identifies each bar whose <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value equals either visible bars’ timestamp and colors it on the chart:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Visible-bar-times-1.Cm54dJVW_rV6wP.webp" alt="image" width="1336" height="642" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Visible bar times demo&quot;, overlay = true) // Create strings on the first chart bar that contain the first and last visible bars&#39; UNIX timestamps. var string leftTimestampText = str.format(&quot;UNIX timestamp: {0,number,#}&quot;, chart.left_visible_bar_time) var string rightTimestampText = str.format(&quot;UNIX timestamp: {0,number,#}&quot;, chart.right_visible_bar_time) // Create strings on the first bar that contain the exchange date and time of the visible bars in ISO 8601 format. var string leftDateTimeText = &quot;Date and time: &quot; + str.format_time(chart.left_visible_bar_time) var string rightDateTimeText = &quot;Date and time: &quot; + str.format_time(chart.right_visible_bar_time) //@variable A `label` object anchored to the first visible bar&#39;s time. Shows the `leftTimestampText`. var label leftTimeLabel = label.new( chart.left_visible_bar_time, 0, leftTimestampText, xloc.bar_time, yloc.abovebar, color.purple, label.style_label_lower_left, color.white, size.large ) //@variable A `label` object anchored to the last visible bar&#39;s time. Shows the `rightTimestampText`. var label rightTimeLabel = label.new( chart.right_visible_bar_time, 0, rightTimestampText, xloc.bar_time, yloc.abovebar, color.teal, label.style_label_lower_right, color.white, size.large ) //@variable A single-cell `table` object showing the `leftDateTimeText`. var table leftTimeTable = table.new(position.middle_left, 1, 1, color.purple) //@variable A single-cell `table` object showing the `rightDateTimeText`. var table rightTimeTable = table.new(position.middle_right, 1, 1, color.teal) // On the first bar, initialize the `leftTimeTable` and `rightTimeTable` with the corresponding date-time text. if barstate.isfirst leftTimeTable.cell(0, 0, leftDateTimeText, text_color = color.white, text_size = size.large) rightTimeTable.cell(0, 0, rightDateTimeText, text_color = color.white, text_size = size.large) //@variable Is purple at the left visible bar&#39;s opening time, teal at the right bar&#39;s opening time, `na` otherwise. color barColor = switch time chart.left_visible_bar_time =&gt; color.purple chart.right_visible_bar_time =&gt; color.teal // Color the leftmost and rightmost visible bars using the `barColor`. barcolor(barColor, title = &quot;Leftmost and rightmost visible bar color&quot;)</div> <p>Note that:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.left_visible_bar_time">chart.left_visible_bar_time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_chart.right_visible_bar_time">chart.right_visible_bar_time</a> values are consistent across all executions, which allows the script to identify the visible bars’ timestamps on the <em>first</em> bar and check when the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> value equals them. The script restarts on any chart window changes, updating the variables’ series to reference the new timestamps on every bar.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function uses ISO 8601 format by default when the call does not include a <code dir="auto">format</code> argument because it is the <em>international standard</em> for expressing dates and times. See the <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">Formatting dates and times</a> section to learn more about time string formats.</li> </ul> <h3 id="syminfotimezone" class="md-heading"><a href="#syminfotimezone">`syminfo.timezone`<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> variable holds a <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> representing the current symbol’s <em>exchange</em> time zone. The “string” value expresses the time zone as an <em>IANA identifier</em> (e.g., <code dir="auto">&quot;America/New_York&quot;</code>). The overloads of <a href="/pine-script-docs/concepts/time/#time-functions">time functions</a> that include a <code dir="auto">timezone</code> parameter use <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> as the default argument.</p> <p>Because this variable is the default <code dir="auto">timezone</code> argument for all applicable time function overloads, it is unnecessary to use as an explicit argument, except for stylistic purposes. However, programmers can use the variable in other ways, such as:</p> <ul> <li>Displaying the “string” in <a href="/pine-script-docs/writing/debugging/#pine-logs">Pine Logs</a> or <a href="/pine-script-docs/language/type-system/#drawing-types">drawings</a> to inspect the exchange time zone’s IANA identifier.</li> <li>Comparing the value to other time zone strings to create time zone-based conditional logic.</li> <li>Requesting the exchange time zones of other symbols with <code dir="auto">request.*()</code> function calls.</li> </ul> <p>The following script uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> variable to retrieve the <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> of its latest execution. It <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formats</a> the timestamp into date-time strings expressed in the main symbol’s exchange time zone and a requested symbol’s exchange time zone, which it displays along with the IANA identifiers in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> on the last chart bar:</p> <p><img src="/pine-script-docs/_astro/Time-Time-variables-Syminfo-timezone-1.BAiDPB0L_Z1tzhco.webp" alt="image" width="1336" height="580" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`syminfo.timezone` demo&quot;, overlay = true, dynamic_requests = true) //@variable The symbol to request exchange time zone information for. string symbolInput = input.symbol(&quot;NSE:BANKNIFTY&quot;, &quot;Requested symbol&quot;) //@variable A `table` object displaying the exchange time zone and the time of the script&#39;s execution. var table t = table.new(position.bottom_right, 2, 3, color.yellow, border_color = color.black, border_width = 1) //@variable The IANA identifier of the exchange time zone requested for the `symbolInput` symbol. var string requestedTimezone = na if barstate.islastconfirmedhistory // Retrieve the time zone of the user-specified symbol&#39;s exchange. requestedTimezone := request.security(symbolInput, &quot;&quot;, syminfo.timezone, calc_bars_count = 2) // Initialize the `t` table&#39;s header cells. t.cell(0, 0, &quot;Exchange prefix and time zone string&quot;, text_size = size.large) t.cell(1, 0, &quot;Last execution date and time&quot;, text_size = size.large) t.cell(0, 1, syminfo.prefix(syminfo.tickerid) + &quot; (&quot; + syminfo.timezone + &quot;)&quot;, text_size = size.large) t.cell(0, 2, syminfo.prefix(symbolInput) + &quot; (&quot; + requestedTimezone + &quot;)&quot;, text_size = size.large) if barstate.islast //@variable The formatting string for all `str.format_time()` calls. var string formatString = &quot;HH:mm:ss &#39;on&#39; MMM dd, YYYY&quot; // Initialize table cells to display the formatted text. t.cell(1, 1, str.format_time(timenow, formatString), text_size = size.large) t.cell(1, 2, str.format_time(timenow, formatString, requestedTimezone), text_size = size.large)</div> <p>Note that:</p> <ul> <li>Pine scripts execute on realtime bars only when new updates occur in the data feed, and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> updates only on script executions. As such, when no realtime updates are available, the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> timestamp does not change. See <a href="/pine-script-docs/concepts/time/#timenow">this section</a> above for more information.</li> <li>The default <code dir="auto">symbolInput</code> value is <code dir="auto">&quot;NSE:BANKNIFTY&quot;</code>. NSE is in the “Asia/Kolkata” <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>, which is 9.5 hours ahead of the main symbol’s exchange time zone (“America/New_York”) at the time of the screenshot. Although the <em>local</em> time representations differ, both refer to the same <em>absolute</em> time that the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a> timestamp represents.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_indicator">indicator()</a> declaration statement includes <code dir="auto">dynamic_requests = true</code>, which allows the script to call <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_request.security">request.security()</a> dynamically inside the <a href="https://www.tradingview.com/pine-script-reference/v6/#kw_if">if</a> structure’s <em>local scope</em>. See the <a href="/pine-script-docs/concepts/other-timeframes-and-data/#dynamic-requests">Dynamic requests</a> section of the <a href="/pine-script-docs/concepts/other-timeframes-and-data/">Other timeframes and data</a> page to learn more.</li> </ul> <h2 id="time-functions" class="md-heading"><a href="#time-functions">Time functions<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Pine Script™ features several built-in functions that scripts can use to retrieve, calculate, and express time values:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> functions allow scripts to retrieve <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> for the opening and closing times of bars within a session on a specified timeframe, without requiring <code dir="auto">request.*()</code> function calls.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_year">year()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_weekofyear">weekofyear()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_minute">minute()</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_second">second()</a> functions calculate calendar-based values, expressed in a specified <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>, from a UNIX timestamp.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function calculates a UNIX timestamp from a specified calendar date and time.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function formats a UNIX timestamp into a human-readable date/time “string”, expressed in a specified time zone. The <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">Formatting dates and times</a> section below provides detailed information about formatting timestamps with this function.</li> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_input.time">input.time()</a> function returns a UNIX timestamp corresponding to the user-specified date and time, and the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_input.session">input.session()</a> function returns a valid <a href="/pine-script-docs/concepts/sessions/#session-strings">session string</a> corresponding to the user-specified start and end times. See the <a href="/pine-script-docs/concepts/inputs/#time-input">Time input</a> and <a href="/pine-script-docs/concepts/inputs/#session-input">Session input</a> sections of the <a href="/pine-script-docs/concepts/inputs/">Inputs</a> page to learn more about these functions.</li> </ul> <h3 id="time-and-time_close-functions" class="md-heading"><a href="#time-and-time_close-functions">`time()` and `time_close()` functions<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> functions return <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> representing the opening and closing times of bars on a specified timeframe. Both functions can <em>filter</em> their returned values based on a given <a href="/pine-script-docs/concepts/sessions/">session</a> in a specific <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>. They each have the following signatures:</p> <div class="expressive-code not-content"><link rel="stylesheet" href="/pine-script-docs/_astro/ec.dtidy.css"/><script type="module" src="/pine-script-docs/_astro/ec.qopfj.js"></script><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">functionName(timeframe, bars_back) → series int</span></div></div><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">functionName(timeframe, session, bars_back) → series int</span></div></div><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">functionName(timeframe, session, timezone, bars_back) → series int</span></div></div></code></pre></figure></div> <p>Where:</p> <ul> <li><code dir="auto">functionName</code> is the function’s identifier.</li> <li>The <code dir="auto">timeframe</code> parameter accepts a <a href="/pine-script-docs/concepts/timeframes/#timeframe-string-specifications">timeframe string</a>. The function uses the script’s main timeframe if the argument is <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timeframe.period">timeframe.period</a> or an empty “string”.</li> <li>The <code dir="auto">session</code> parameter accepts a <a href="/pine-script-docs/concepts/sessions/#session-strings">session string</a> defining the session’s start and end times (e.g., <code dir="auto">&quot;0930-1600&quot;</code>) and the days for which it applies (e.g., <code dir="auto">&quot;:23456&quot;</code> means Monday - Friday). If the value does not specify the days, the session applies to <em>all</em> weekdays automatically. The function returns UNIX timestamps only for the bars <em>within</em> the session. It returns <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> if a bar’s time is <em>outside</em> the session. If the <code dir="auto">session</code> argument is an empty “string” or not specified, The function uses the symbol’s session information.</li> <li>The <code dir="auto">timezone</code> parameter accepts a valid <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> that defines the time zone of the specified <code dir="auto">session</code>. It does <strong>not</strong> change the meaning of returned UNIX timestamps, as they are <em>time zone-agnostic</em>. If the <code dir="auto">timezone</code> argument is not specified, the function uses the exchange time zone (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a>).</li> <li>The <code dir="auto">bars_back</code> parameter accepts an “int” value specifying which bar’s time the returned timestamp represents. If the value is positive, the function returns the timestamp from that number of bars back relative to the current bar. If the value is negative and greater than or equal to -500, the function returns the <em>expected</em> timestamp of a future bar. The default value is 0.</li> </ul> <p>Similar to the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> variables, these functions behave differently on <em>time-based</em> and <em>non-time-based</em> charts.</p> <p>Time-based charts have bars that open and close at <em>predictable</em> times, whereas the bars on <a href="https://www.tradingview.com/support/solutions/43000709225/">tick charts</a> and all <a href="/pine-script-docs/concepts/non-standard-charts-data/">non-standard charts</a>, excluding <a href="https://www.tradingview.com/support/solutions/43000619436/">Heikin Ashi</a>, open and close at irregular, <em>unpredictable</em> times. Consequently, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> cannot calculate the expected closing times of realtime bars on non-time-based charts, so it returns <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> on those bars. Similarly, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> function with a negative <code dir="auto">bars_back</code> value cannot accurately calculate the expected opening time of a future realtime bar on these charts. See the <em>second example</em> in <a href="/pine-script-docs/concepts/time/#time-and-time_close-variables">this section</a> above. That example script exhibits the same behavior on a price-based chart if it uses a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close(&quot;&quot;)</a> call instead of the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> variable.</p> <p>Typical use cases for the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> functions include:</p> <ul> <li>Testing for bars that open or close in <a href="/pine-script-docs/concepts/time/#testing-for-sessions">specific sessions</a> defined by the <code dir="auto">session</code> and <code dir="auto">timezone</code> parameters.</li> <li><a href="/pine-script-docs/concepts/time/#testing-for-changes-in-higher-timeframes">Testing for changes</a> or measuring time differences on specified higher timeframes.</li> </ul> <h4 id="testing-for-sessions" class="md-heading"><a href="#testing-for-sessions">Testing for sessions<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h4> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> functions’ <code dir="auto">session</code> and <code dir="auto">timezone</code> parameters define the <a href="/pine-script-docs/concepts/sessions/">sessions</a> for which they can return <em>non-na</em> values. If a call to either function references a bar that opens/closes within the defined session in a given <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>, it returns a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> for that bar. Otherwise, it returns <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a>. Programmers can pass the returned values to the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_na">na()</a> function to identify which bars open or close within specified intervals, which is helpful for session-based calculations and logic.</p> <p>This simple script identifies when a bar on the chart’s timeframe opens at or after 11:00 and before 13:00 in the exchange time zone on any trading day. It calls <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> with <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timeframe.period">timeframe.period</a> as the <code dir="auto">timeframe</code> argument and the <code dir="auto">&quot;1100-1300&quot;</code> <a href="/pine-script-docs/concepts/sessions/#session-strings">session string</a> as the <code dir="auto">session</code> argument, and then verifies whether the returned value is <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a> with the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_na">na()</a> function. When the value is <strong>not</strong> <a href="https://www.tradingview.com/pine-script-reference/v6/#var_na">na</a>, the script highlights the chart’s background to indicate that the bar opened in the session:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Time-and-time-close-Testing-for-sessions-1.CJeOvGxZ_Z1sBkKc.webp" alt="image" width="1458" height="534" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Testing for session bars demo&quot;, overlay = true) //@variable Checks if the bar opens between 11:00 and 13:00. Is `true` if the `time()` function does not return `na`. bool inSession = not na(time(timeframe.period, &quot;1100-1300&quot;)) // Highlight the background of the bars that are in the session &quot;11:00-13:00&quot;. bgcolor(inSession ? color.rgb(155, 39, 176, 80) : na)</div> <p>Note that:</p> <ul> <li>The <code dir="auto">session</code> argument in the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> call represents an interval in the <em>exchange</em> time zone because <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> is the default <code dir="auto">timezone</code> argument.</li> <li>The session string expresses the start and end times in the <code dir="auto">&quot;HHmm-HHmm&quot;</code> format, where <code dir="auto">&quot;HH&quot;</code> is the two-digit <em>hour</em> and <code dir="auto">&quot;mm&quot;</code> is the two-digit <em>minute</em>. Session strings can also specify the <em>weekdays</em> a session applies to. However, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> call’s <code dir="auto">session</code> argument (<code dir="auto">&quot;1100-1300&quot;</code>) does not include this information, which is why it considers the session valid for <em>every</em> day. See the <a href="/pine-script-docs/concepts/sessions/">Sessions</a> page to learn more.</li> </ul> <p>When using session strings in <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> calls, it’s crucial to understand that such strings define start and end times in a <em>specific</em> <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>. The local hour and minute in one region may not correspond to the same point in UNIX time as that same hour and minute in another region. Therefore, calls to these functions with different <code dir="auto">timezone</code> arguments can return non-na timestamps at <em>different</em> times, as the specified <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> changes the meaning of the local times represented in the <code dir="auto">session</code> argument.</p> <p>This example demonstrates how the <code dir="auto">timezone</code> parameter affects the <code dir="auto">session</code> parameter in a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> function call. The script calculates an <code dir="auto">opensInSession</code> condition that uses a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> call with arguments based on <a href="/pine-script-docs/concepts/inputs/">inputs</a>. The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_input.session">session input</a> for the <code dir="auto">session</code> argument includes four preset options: <code dir="auto">&quot;0000-0400&quot;</code>, <code dir="auto">&quot;0930-1400&quot;</code>, <code dir="auto">&quot;1300-1700&quot;</code>, and <code dir="auto">&quot;1700-2100&quot;</code>. The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_input.string">string input</a> that defines the <code dir="auto">timezone</code> argument includes four IANA time zone options representing different offsets from UTC: <code dir="auto">&quot;America/Vancouver&quot;</code> (UTC-7/-8), <code dir="auto">&quot;America/New_York&quot;</code> (UTC-4/-5), <code dir="auto">&quot;Asia/Dubai&quot;</code> (UTC+4), and <code dir="auto">&quot;Austrailia/Sydney&quot;</code> (UTC+10/+11).</p> <p>For any chosen <code dir="auto">sessionInput</code> value, changing the <code dir="auto">timezoneInput</code> value changes the specified session’s time zone. The script highlights <em>different bars</em> with each time zone choice because, unlike <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a>, the <em>absolute</em> times that local hour and minute values correspond to <em>varies</em> across time zones:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Time-and-time-close-Testing-for-sessions-2.DKRpvO46_Z1pg5HR.webp" alt="image" width="1566" height="570" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Testing session time zones demo&quot;, overlay = true) //@variable The timeframe of the analyzed bars. string timeframeInput = input.timeframe(&quot;60&quot;, &quot;Timeframe&quot;) //@variable The session to check. Features four interval options. string sessionInput = input.session(&quot;1300-1700&quot;, &quot;Session&quot;, [&quot;0000-0400&quot;, &quot;0930-1400&quot;, &quot;1300-1700&quot;, &quot;1700-2100&quot;]) //@variable An IANA identifier representing the time zone of the `sessionInput`. Fetures four preset options. string timezoneInput = input.string(&quot;America/New_York&quot;, &quot;Time zone&quot;, [&quot;America/Vancouver&quot;, &quot;America/New_York&quot;, &quot;Asia/Dubai&quot;, &quot;Australia/Sydney&quot;] ) //@variable Is `true` if a `timeframeInput` bar opens within the `sessionInput` session in the `timezoneInput` time zone. // The condition detects the session on different bars, depending on the chosesn time zone, because identical // local times in different time zones refer to different absolute points in UNIX time. bool opensInSession = not na(time(timeframeInput, sessionInput, timezoneInput)) // Highlight the background when `opensInSession` is `true`. bgcolor(opensInSession ? color.rgb(33, 149, 243, 80) : na, title = &quot;Open in session highlight&quot;)</div> <p>Note that:</p> <ul> <li>This script uses <em>IANA notation</em> for all <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone strings</a> because it is the recommended format. Using an IANA identifier allows the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> call to automatically adjust the session’s UTC offset based on a region’s local time policies, such as daylight saving time.</li> </ul> <h4 id="testing-for-changes-in-higher-timeframes" class="md-heading"><a href="#testing-for-changes-in-higher-timeframes">Testing for changes in higher timeframes<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h4> <p>The <code dir="auto">timeframe</code> parameter of the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> functions specifies the timeframe of the bars in the calculation, allowing scripts to retrieve opening/closing <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> from <em>higher timeframes</em> than the current chart’s timeframe without requiring <code dir="auto">request.*()</code> function calls.</p> <p>Programmers can use the opening/closing timestamps from higher-timeframe (HTF) bars to detect timeframe changes. One common approach is to call <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> or <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> with a consistent <code dir="auto">timeframe</code> argument across all executions on a time-based chart and measure the one-bar change in the returned value with the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_ta.change">ta.change()</a> function. The result is a <em>nonzero</em> value only when an HTF bar opens. One can also check whether the data has a time gap at that point by comparing the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> value to the previous bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> value. A gap is present when the opening timestamp on the current bar is greater than the closing timestamp on the previous bar.</p> <p>The script below calls <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time(“1M”)</a> to get the opening UNIX timestamp of the current bar on the “1M” timeframe. It detects when bars on that timeframe open by checking when the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_ta.change">ta.change()</a> of the timestamp returns a value greater than 0. On each occurrence of the condition, the script detects whether the HTF bar opened after a gap by checking if the opening time is greater than the previous bar’s <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close(“1M”)</a> value.</p> <p>The script draws <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a> containing <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> “1M” opening times to indicate the chart bars that mark the start of monthly bars. If a monthly bar opens without a gap from the previous closing time, the script draws a blue label. If a monthly bar starts after a gap, it draws a red label. Additionally, if the “1M” opening time does not match the opening time of the chart bar, the script displays that bar’s formatted time in the label for comparison:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Time-and-time-close-Testing-for-changes-in-higher-timeframes-1.EwKXV91R_16xSC0.webp" alt="image" width="1336" height="640" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Detecting changes in higher timeframes demo&quot;, overlay = true) //@variable The opening UNIX timestamp of the current bar on the &quot;1M&quot; timeframe. int currMonthlyOpenTime = time(&quot;1M&quot;) //@variable The closing timestamp on the &quot;1M&quot; timeframe as of the previous chart bar. int prevMonthlyCloseTime = time_close(&quot;1M&quot;)[1] //@variable Is `true` when the opening time on the &quot;1M&quot; timeframe changes, indicating a new monthly bar. bool isNewTf = ta.change(currMonthlyOpenTime) &gt; 0 if isNewTf // Initialize variables for the `text` and `color` properties of the label drawing. string lblText = &quot;New &#39;1M&#39; opening time:\n&quot; + str.format_time(currMonthlyOpenTime) color lblColor = color.blue //@variable Is `true` when the `currMonthlyOpenTime` exceeds the `prevMonthlyCloseTime`, indicating a time gap. bool hasGap = currMonthlyOpenTime &gt; prevMonthlyCloseTime // Modify the `lblText` and `lblColor` based on the `hasGap` value. if hasGap lblText := &quot;Gap from previous &#39;1M&#39; close.\n\n&quot; + lblText lblColor := color.red // Include the formatted `time` value if the `currMonthlyOpenTime` is before the first available chart bar&#39;s `time`. if time &gt; currMonthlyOpenTime lblText += &quot;\nFirst chart bar has a later time:\n&quot; + str.format_time(time) // Draw a `lblColor` label anchored to the `time` to display the `lblText`. label.new( time, high, lblText, xloc.bar_time, color = lblColor, style = label.style_label_lower_right, textcolor = color.white, size = size.large )</div> <p>Note that:</p> <ul> <li>Using <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_ta.change">ta.change()</a> on a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> or <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> call’s result is not the <em>only</em> way to detect changes in a higher timeframe. The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timeframe.change">timeframe.change()</a> function is an equivalent, more convenient option for scripts that do not need to <em>use</em> the UNIX timestamps from HTF bars in other calculations, as it returns a “bool” value directly without extra code.</li> <li>The detected monthly opening times do not always correspond to the first calendar day of the month. Instead, they correspond to the first time assigned to a “1M” bar, which can be <em>after</em> the first calendar day. For symbols with overnight sessions, such as “USDJPY” in our example chart, a “1M” bar can also open <em>before</em> the first calendar day.</li> <li>Sometimes, the opening time assigned to an HTF bar might <em>not</em> equal the opening time of any chart bar, which is why other conditions such as <code dir="auto">time == time(&quot;1M&quot;)</code> cannot detect new monthly bars consistently. For example, on our “USDJPY” chart, the “1M” opening time <code dir="auto">2023-12-31T17:00:00-0500</code> does not match an opening time on the “1D” timeframe. The first available “1D” bar after that point opened at <code dir="auto">2024-01-01T17:00:00-0500</code>.</li> </ul> <h3 id="calendar-based-functions" class="md-heading"><a href="#calendar-based-functions">Calendar-based functions<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_year">year()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_weekofyear">weekofyear()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_hour">hour()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_minute">minute()</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_second">second()</a> functions calculate <em>calendar-based</em> “int” values from a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a>. Unlike the <a href="/pine-script-docs/concepts/time/#calendar-based-variables">calendar-based variables</a>, which always hold exchange calendar values based on the current bar’s opening timestamp, these functions can return calendar values for any valid timestamp and express them in a chosen <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>.</p> <p>Each of these calendar-based functions has the following two signatures:</p> <div class="expressive-code not-content"><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">functionName(time) → series int</span></div></div><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">functionName(time, timezone) → series int</span></div></div></code></pre></figure></div> <p>Where:</p> <ul> <li><code dir="auto">functionName</code> is the function’s identifier.</li> <li>The <code dir="auto">time</code> parameter accepts an “int” UNIX timestamp for which the function calculates a corresponding calendar value.</li> <li>The <code dir="auto">timezone</code> parameter accepts a <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> specifying the returned value’s time zone. If the <code dir="auto">timezone</code> argument is not specified, the function uses the exchange time zone (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a>).</li> </ul> <p>In contrast to the functions that return UNIX timestamps, a calendar-based function returns different “int” results for various time zones, as calendar values represent parts of a <em>local time</em> in a <em>specific region</em>.</p> <p>For instance, the simple script below uses two calls to <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> to calculate each bar’s opening day in the exchange time zone and the “Australia/Sydney” time zone. It plots the results of the two calls in a separate pane for comparison:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Calendar-based-functions-1.7yz46qU5_56taF.webp" alt="image" width="1792" height="736" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`dayofmonth()` demo&quot;, overlay = false) //@variable An &quot;int&quot; representing the current bar&#39;s opening calendar day in the exchange time zone. // Equivalent to the `dayofmonth` variable. int openingDay = dayofmonth(time) //@variable An &quot;int&quot; representing the current bar&#39;s opening calendar day in the &quot;Australia/Sydney&quot; time zone. int openingDaySydney = dayofmonth(time, &quot;Australia/Sydney&quot;) // Plot the calendar day values. plot(openingDay, &quot;Day of Month (Exchange)&quot;, linewidth = 6, color = color.blue) plot(openingDaySydney, &quot;Day of Month (Sydney)&quot;, linewidth = 3, color = color.orange)</div> <p>Note that:</p> <ul> <li>The first <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> call calculates the bar’s opening day in the exchange time zone because it does not include a <code dir="auto">timezone</code> argument. This call returns the same value that the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_dayofmonth">dayofmonth</a> variable references.</li> <li>Our example symbol’s exchange time zone is “America/New_York”, which follows UTC-5 during standard time and UTC-4 during daylight saving time (DST). The “Australia/Sydney” time zone follows UTC+10 during standard time and UTC+11 during DST. However, Sydney observes DST at <em>different</em> times of the year than New York. As such, its time zone is 14, 15, or 16 hours ahead of the exchange time zone, depending on the time of year. The plots on our “1D” chart diverge when the difference is at least 15 hours because the bars open at 09:30 in exchange time, and 15 hours ahead is 00:30 on the <em>next</em> calendar day.</li> </ul> <p>It’s important to understand that although the <code dir="auto">time</code> argument in a calendar-based function call represents a single, absolute point in time, each function returns only <em>part</em> of the date and time information available from the timestamp. Consequently, a calendar-based function’s returned value does <strong>not</strong> directly correspond to a <em>unique</em> time point, and conditions based on individual calendar values can apply to <em>multiple</em> bars.</p> <p>For example, this script uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function to calculate a UNIX timestamp from a date “string”, and it calculates the calendar day from that timestamp, in the exchange time zone, with the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> function. The script compares each bar’s opening day to the calculated day and highlights the background when the two are equal:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Calendar-based-functions-2.CLs3hZaQ_Z1mTUCj.webp" alt="image" width="1468" height="494" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`dayofmonth()` demo&quot;, overlay = false) //@variable The UNIX timestamp corresponding to August 29, 2024 at 00:00:00 UTC. const int fixedTimestamp = timestamp(&quot;29 Aug 2024&quot;) //@variable The day of the month calculated from the `fixedTimestamp`, expressed in the exchange time zone. // If the exchange time zone has a negative UTC offset, this variable&#39;s value is 28 instead of 29. int dayFromTimestamp = dayofmonth(fixedTimestamp) //@variable An &quot;int&quot; representing the current bar&#39;s opening calendar day in the exchange time zone. // Equivalent to the `dayofmonth` variable. int openingDay = dayofmonth(time) // Plot the `openingDay`. plot(openingDay, &quot;Opening day of month&quot;, linewidth = 3) // Highlight the background when the `openingDay` equals the `dayFromTimestamp`. bgcolor(openingDay == dayFromTimestamp ? color.orange : na, title = &quot;Day detected highlight&quot;)</div> <p>Note that:</p> <ul> <li>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call treats its argument as a <em>UTC</em> calendar date because its <code dir="auto">dateString</code> argument does not specify time zone information. However, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> call calculates the day in the <em>exchange time zone</em>. Our example symbol’s exchange time zone is “America/New_York” (UTC-4/-5). Therefore, the returned value on this chart is 28 instead of 29.</li> <li>The script highlights <em>any</em> bar on our chart that opens on the 28th day of <em>any</em> month instead of only a specific bar because the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofmonth">dayofmonth()</a> function’s returned value does <strong>not</strong> represent a specific point in time on its own.</li> <li>This script highlights the bars that <em>open</em> on the day of the month calculated from the timestamp. However, some months on our chart have no trading activity on that day. For example, the script does not highlight when the July 28, 2024 occurs on our chart because NASDAQ is closed on Sundays.</li> </ul> <p>Similar to <a href="/pine-script-docs/concepts/time/#calendar-based-variables">calendar-based variables</a>, these functions are also helpful when testing for dates/times and detecting calendar changes on the chart. The example below uses the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_year">year()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_month">month()</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_weekofyear">weekofyear()</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a> functions on the <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> timestamp to create conditions that test if the current bar is the first bar that closes in a new year, quarter, month, week, and day. The script uses <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_plotshape">plotted shapes</a>, <a href="/pine-script-docs/concepts/text-and-shapes/#labels">labels</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_bgcolor">background colors</a> to visualize the conditions on the chart:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Calendar-based-functions-3.BgN06lg1_1QVzL3.webp" alt="image" width="1336" height="640" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Calendar changes demo&quot;, overlay = true, max_labels_count = 500) // Calculate the year, month, week of year, and day of week corresponding to the `time_close` UNIX timestamp. // All values are expressed in the exchange time zone. int closeYear = year(time_close) int closeMonth = month(time_close) int closeWeekOfYear = weekofyear(time_close) int closeDayOfWeek = dayofweek(time_close) //@variable Is `true` when the change in `closeYear` exceeds 0, marking the first bar that closes in a new year. bool closeInNewYear = ta.change(closeYear) &gt; 0 //@variable Is `true` when the difference in `closeMonth` is not 0, marking the first bar that closes in a new month. bool closeInNewMonth = closeMonth - closeMonth[1] != 0 //@variable Is `true` when `closeMonth - 1` becomes divisible by 3, marking the first bar that closes in a new quarter. bool closeInNewQuarter = (closeMonth[1] - 1) % 3 != 0 and (closeMonth - 1) % 3 == 0 //@variable Is `true` when the change in `closeWeekOfYear` is not 0, marking the first bar that closes in a new week. bool closeInNewWeek = ta.change(closeWeekOfYear) != 0 //@variable Is `true` when the `closeDayOfWeek` changes, marking the first bar that closes in the new day. bool closeInNewDay = closeDayOfWeek != closeDayOfWeek[1] //@variable Switches between `true` and `false` after every `closeInNewDay` occurrence for background color calculation. var bool alternateDay = true if closeInNewDay alternateDay := not alternateDay // Draw a label above the bar to display the `closeWeekOfYear` when `closeInNewWeek` is `true`. if closeInNewWeek label.new( time, 0, &quot;W&quot; + str.tostring(closeWeekOfYear), xloc.bar_time, yloc.abovebar, color.purple, textcolor = color.white, size = size.normal ) // Plot label shapes at the bottom and top of the chart for the `closeInNewYear` and `closeInNewMonth` conditions. plotshape( closeInNewYear, &quot;Close in new year&quot;, shape.labelup, location.bottom, color.teal, text = &quot;New year&quot;, textcolor = color.white, size = size.huge ) plotshape( closeInNewMonth, &quot;Close in new month&quot;, shape.labeldown, location.top, text = &quot;New month&quot;, textcolor = color.white, size = size.large ) // Plot a triangle below the chart bar when `closeInNewQuarter` occurs. plotshape( closeInNewQuarter, &quot;Close in new quarter&quot;, shape.triangleup, location.belowbar, color.maroon, text = &quot;New quarter&quot;, textcolor = color.maroon, size = size.large ) // Highlight the background in alternating colors based on occurrences of `closeInNewDay`. bgcolor(alternateDay ? color.new(color.aqua, 80) : color.new(color.fuchsia, 80), title = &quot;Closing day change&quot;)</div> <p>Note that:</p> <ul> <li>This script’s conditions check for the first bar that closes after each calendar unit changes its value. The bar where each condition is <code dir="auto">true</code> varies with the data available on the chart. For example, the <code dir="auto">closeInNewMonth</code> condition can be <code dir="auto">true</code> <em>after</em> the first calendar day of the month if a chart bar did not close on that day.</li> <li>To detect when new bars start on a specific <em>timeframe</em> rather than strictly calendar changes, check when the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_ta.change">ta.change()</a> of a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time">time()</a> or <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close()</a> call’s returned value is nonzero, or use the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timeframe.change">timeframe.change()</a> function. See <a href="/pine-script-docs/concepts/time/#testing-for-changes-in-higher-timeframes">this section</a> above for more information.</li> </ul> <h3 id="timestamp" class="md-heading"><a href="#timestamp">`timestamp()`<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function calculates a <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> from a specified calendar date and time. It has the following three signatures:</p> <div class="expressive-code not-content"><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">timestamp(year, month, day, hour, minute, second) → simple/series int</span></div></div><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">timestamp(timezone, year, month, day, hour, minute, second) → simple/series int</span></div></div><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">timestamp(dateString) → const int</span></div></div></code></pre></figure></div> <p>The first two signatures listed include <code dir="auto">year</code>, <code dir="auto">month</code>, <code dir="auto">day</code>, <code dir="auto">hour</code>, <code dir="auto">minute</code>, and <code dir="auto">second</code> parameters that accept “int” values defining the calendar date and time. A <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call with either signature must include <code dir="auto">year</code>, <code dir="auto">month</code>, and <code dir="auto">day</code> arguments. The other parameters are optional, each with a default value of 0. Both signatures can return either <em>“simple”</em> or <em>“series”</em> values, depending on the <a href="/pine-script-docs/language/type-system/#qualifiers">qualified types</a> of the specified arguments.</p> <p>The primary difference between the first two signatures is the <code dir="auto">timezone</code> parameter, which accepts a <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> that determines the <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a> of the <em>date and time</em> specified by the other parameters. If a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call with “int” calendar arguments does not include a <code dir="auto">timezone</code> argument, it uses the exchange time zone (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a>) by default.</p> <p>The third signature listed has only <em>one</em> parameter, <code dir="auto">dateString</code>, which accepts a “string” representing a valid calendar date (e.g., <code dir="auto">&quot;20 Aug 2024&quot;</code>). The value can also include the time of day and time zone (e.g., <code dir="auto">&quot;20 Aug 2024 00:00:00 UTC+0&quot;</code>). If the <code dir="auto">dateString</code> argument does not specify the time of day, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call considers the time 00:00 (midnight).</p> <p>Unlike the other two signatures, the default time zone for the third signature is <strong>GMT+0</strong>. It does <strong>not</strong> use the exchange time zone by default because it interprets time zone information from the <code dir="auto">dateString</code> directly. Additionally, the third signature is the only one that returns a <em>“const int”</em> value. As shown in the <a href="/pine-script-docs/concepts/inputs/#time-input">Time input</a> section of the <a href="/pine-script-docs/concepts/inputs/">Inputs</a> page, programmers can use this overload’s returned value as the <code dir="auto">defval</code> argument in an <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_input.time">input.time()</a> function call.</p> <p>When using the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> function, it’s crucial to understand how time zone information affects its calculations. The <em>absolute</em> point in time represented by a specific calendar date <em>depends</em> on its time zone, as an identical date and time in various time zones can refer to <strong>different</strong> amounts of time elapsed since the <em>UNIX Epoch</em>. Therefore, changing the time zone of the calendar date and time in a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call <em>can change</em> its returned <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a>.</p> <p>The following script compares the results of four different <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> calls that evaluate the date 2021-01-01 in different time zones. The first <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call does not specify time zone information in its <code dir="auto">dateString</code> argument, so it treats the value as a <em>UTC</em> calendar date. The fourth call also evaluates the calendar date in UTC because it includes <code dir="auto">&quot;UTC0&quot;</code> as the <code dir="auto">timezone</code> argument. The second <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call uses the first signature listed above, meaning it uses the exchange time zone, and the third call uses the second signature with <code dir="auto">&quot;America/New_York&quot;</code> as the <code dir="auto">timezone</code> argument.</p> <p>The script draws a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> with rows displaying each <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> call, its assigned variable, the calculated UNIX timestamp, and a <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> representation of the time. As we see on the “NASDAQ:MSFT” chart below, the first and fourth table rows show <em>different</em> timestamps than the first and third, leading to different formatted strings in the last column:</p> <p><img src="/pine-script-docs/_astro/Time-Time-functions-Timestamp-1.CFLoCdL8_A5DD7.webp" alt="image" width="1378" height="472" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;`timestamp()` demo&quot;, overlay = false) //@variable A `table` that displays the different `timestamp()` calls, their returned timestamps, and formatted results. var table displayTable = table.new( position.middle_center, 4, 5, color.white, border_color = color.black, border_width = 2 ) //@function Initializes a `displayTable` cell showing the `displayText` with an optional `specialFormat`. printCell(int colID, int rowID, string displayText, string specialFormat = &quot;&quot;) =&gt; displayTable.cell(colID, rowID, displayText, text_size = size.large) switch specialFormat &quot;header&quot; =&gt; displayTable.cell_set_bgcolor(colID, rowID, color.rgb(76, 175, 79, 70)) &quot;code&quot; =&gt; displayTable.cell_set_text_font_family(colID, rowID, font.family_monospace) displayTable.cell_set_text_size(colID, rowID, size.normal) displayTable.cell_set_text_halign(colID, rowID, text.align_left) if barstate.islastconfirmedhistory //@variable The UNIX timestamp corresponding to January 1, 2021 in the UTC+0 time zone. int dateTimestamp1 = timestamp(&quot;2021-01-01&quot;) //@variable The UNIX timestamp corresponding to January 1, 2021 in the exchange time zone. int dateTimestamp2 = timestamp(2021, 1, 1, 0, 0) //@variable The UNIX timestamp corresponding to January 1, 2021 in the &quot;America/New_York&quot; time zone. int dateTimestamp3 = timestamp(&quot;America/New_York&quot;, 2021, 1, 1, 0, 0) //@variable The UNIX timestamp corresponding to January 1, 2021 in the &quot;UTC0&quot; (UTC+0) time zone. int dateTimestamp4 = timestamp(&quot;UTC0&quot;, 2021, 1, 1, 0, 0) // Initialize the top header cells in the `displayTable`. printCell(0, 0, &quot;Variable&quot;, &quot;header&quot;) printCell(1, 0, &quot;Function call&quot;, &quot;header&quot;) printCell(2, 0, &quot;Timestamp returned&quot;, &quot;header&quot;) printCell(3, 0, &quot;Formatted date/time&quot;, &quot;header&quot;) // Initialize a table row for `dateTimestamp1` results. printCell(0, 1, &quot;`dateTimestamp1`&quot;, &quot;header&quot;) printCell(1, 1, &quot;`timestamp(\&quot;2021-01-01\&quot;)`&quot;, &quot;code&quot;) printCell(2, 1, str.tostring(dateTimestamp1)) printCell(3, 1, str.format_time(dateTimestamp1, &quot;yyyy.MM.dd HH:mm (Z)&quot;)) // Initialize a table row for `dateTimestamp2` results. printCell(0, 2, &quot;`dateTimestamp2`&quot;, &quot;header&quot;) printCell(1, 2, &quot;`timestamp(2021, 1, 1, 0, 0)`&quot;, &quot;code&quot;) printCell(2, 2, str.tostring(dateTimestamp2)) printCell(3, 2, str.format_time(dateTimestamp2, &quot;yyyy.MM.dd HH:mm (Z)&quot;)) // Initialize a table row for `dateTimestamp3` results. printCell(0, 3, &quot;`dateTimestamp3`&quot;, &quot;header&quot;) printCell(1, 3, &quot;`timestamp(\&quot;America/New_York\&quot;, 2021, 1, 1, 0, 0)`&quot;, &quot;code&quot;) printCell(2, 3, str.tostring(dateTimestamp3)) printCell(3, 3, str.format_time(dateTimestamp3, &quot;yyyy.MM.dd HH:mm (Z)&quot;)) // Initialize a table row for `dateTimestamp4` results. printCell(0, 4, &quot;`dateTimestamp4`&quot;, &quot;header&quot;) printCell(1, 4, &quot;`timestamp(\&quot;UTC0\&quot;, 2021, 1, 1, 0, 0)`&quot;, &quot;code&quot;) printCell(2, 4, str.tostring(dateTimestamp4)) printCell(3, 4, str.format_time(dateTimestamp4, &quot;yyyy.MM.dd HH:mm (Z)&quot;))</div> <p>Note that:</p> <ul> <li>The <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> date-time strings express results in the exchange time zone because the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function uses <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> as the default <code dir="auto">timezone</code> argument. The formatted values on our example chart show the offset string <code dir="auto">&quot;-0500&quot;</code> because NASDAQ’s time zone (“America/New_York”) follows UTC-5 during <em>standard time</em>.</li> <li>The formatted strings on the first and fourth rows show the date and time five hours <em>before</em> January 1, 2021, because the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> calls evaluated the date in <em>UTC</em> and the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> calls used a time zone five hours <em>behind</em> UTC.</li> <li>On our chart, the second and third rows have matching timestamps because both corresponding <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_timestamp">timestamp()</a> calls evaluated the date in the “America/New_York” time zone. The two rows would show different results if we applied the script to a symbol with a different exchange time zone.</li> </ul> <h2 id="formatting-dates-and-times" class="md-heading"><a href="#formatting-dates-and-times">Formatting dates and times<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Programmers can format <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a> into human-readable dates and times, expressed in specific <a href="/pine-script-docs/concepts/time/#time-zones">time zones</a>, using the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function. The function has the following signature:</p> <div class="expressive-code not-content"><figure class="frame"><figcaption class="header"></figcaption><pre data-language="text"><code><div class="ec-line"><div class="code"><span style="--0:#e1e4e8;--1:#24292e">str.format_time(time, format, timezone) → series string</span></div></div></code></pre></figure></div> <p>Where:</p> <ul> <li>The <code dir="auto">time</code> parameter specifies the “int” UNIX timestamp to express as a readable time.</li> <li>The <code dir="auto">format</code> parameter accepts a “string” consisting of <em>formatting tokens</em> that determine the returned information. If the function call does not include a <code dir="auto">format</code> argument, it uses the <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow">ISO 8601</a> standard format: <code dir="auto">&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ssZ&quot;</code>. See the table below for a list of valid tokens and the information they represent.</li> <li>The <code dir="auto">timezone</code> parameter determines the time zone of the formatted result. It accepts a <a href="/pine-script-docs/concepts/time/#time-zone-strings">time zone string</a> in UTC or IANA notation. If the call does not specify a <code dir="auto">timezone</code>, it uses the exchange time zone (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a>).</li> </ul> <p>The general-purpose <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format">str.format()</a> function can also format UNIX timestamps into readable dates and times. However, the function <strong>cannot</strong> express time information in <em>different</em> time zones. It always expresses dates and times in <strong>UTC+0</strong>. In turn, using this function to format timestamps often results in <em>erroneous</em> practices, such as mathematically modifying a timestamp to try and represent the time in another time zone. However, a UNIX timestamp is a unique, <strong>time zone-agnostic</strong> representation of a specific point in time. As such, modifying a UNIX timestamp changes the <em>absolute time</em> it represents rather than expressing the same time in a different time zone.</p> <p>The <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function does not have this limitation, as it can calculate dates and times in <em>any</em> time zone correctly without changing the meaning of a UNIX timestamp. In addition, unlike <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format">str.format()</a>, it is optimized specifically for processing time values. Therefore, we recommend that programmers use <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> instead of <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format">str.format()</a> to format UNIX timestamps into readable dates and times.</p> <p>A <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> call’s <code dir="auto">format</code> argument determines the time information its returned value contains. The function treats characters and sequences in the argument as <em>formatting tokens</em>, which act as <em>placeholders</em> for values in the returned date/time “string”. The following table outlines valid formatting tokens and explains what each represents:</p> <table><thead><tr><th style="text-align:center">Token</th><th style="text-align:center">Represents</th><th style="text-align:left">Remarks and examples</th></tr></thead><tbody><tr><td style="text-align:center"><code dir="auto">&quot;y&quot;</code></td><td style="text-align:center">Year</td><td style="text-align:left">Use <code dir="auto">&quot;yy&quot;</code> to include the last two digits of the year (e.g., <code dir="auto">&quot;00&quot;</code>), or <code dir="auto">&quot;yyyy&quot;</code> to include the complete year number (e.g., <code dir="auto">&quot;2000&quot;</code>).</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;M&quot;</code></td><td style="text-align:center">Month</td><td style="text-align:left">Uppercase <code dir="auto">&quot;M&quot;</code> for the month, not to be confused with <em>lowercase</em> <code dir="auto">&quot;m&quot;</code> for the minute. <br/>Use <code dir="auto">&quot;MM&quot;</code> to include the two-digit month number with a leading zero for single-digit values (e.g., <code dir="auto">&quot;01&quot;</code>), <code dir="auto">&quot;MMM&quot;</code> to include the three-letter abbreviation of month (e.g., <code dir="auto">&quot;Jan&quot;</code>), or <code dir="auto">&quot;MMMM&quot;</code> for the full month name (e.g., <code dir="auto">&quot;January&quot;</code>).</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;d&quot;</code></td><td style="text-align:center">Day of the <strong>month</strong></td><td style="text-align:left">Lowercase <code dir="auto">&quot;d&quot;</code>. <br/>Includes the numeric day of the month (<code dir="auto">&quot;1&quot;</code> to <code dir="auto">&quot;31&quot;</code>). <br/> Use <code dir="auto">&quot;dd&quot;</code> for the two-digit day number with a leading zero for single-digit values. <br/>It is <em>not</em> a placeholder for the day number of the <em>week</em> (1-7). Use <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_dayofweek">dayofweek()</a> to calculate that value.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;D&quot;</code></td><td style="text-align:center">Day of the <strong>year</strong></td><td style="text-align:left">Uppercase <code dir="auto">&quot;D&quot;</code>. <br/> Includes the numeric day of the year (<code dir="auto">&quot;1&quot;</code> to <code dir="auto">&quot;366&quot;</code>). <br/> Use <code dir="auto">&quot;DD&quot;</code> or <code dir="auto">&quot;DDD&quot;</code> for the two-digit or three-digit day number with leading zeros.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;w&quot;</code></td><td style="text-align:center">Week of the <strong>year</strong></td><td style="text-align:left">Lowercase <code dir="auto">&quot;w&quot;</code>. <br/>Includes the week number of the year (<code dir="auto">&quot;1&quot;</code> to <code dir="auto">&quot;53&quot;</code>). <br/>Use <code dir="auto">&quot;ww&quot;</code> for the two-digit week number with a leading zero for single-digit values.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;W&quot;</code></td><td style="text-align:center">Week of the <strong>month</strong></td><td style="text-align:left">Uppercase <code dir="auto">&quot;W&quot;</code>. <br/>Includes the week number of the month (<code dir="auto">&quot;1&quot;</code> to <code dir="auto">&quot;5&quot;</code>).</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;a&quot;</code></td><td style="text-align:center">AM/PM postfix</td><td style="text-align:left">Lowercase <code dir="auto">&quot;a&quot;</code>. <br/>Includes <code dir="auto">&quot;AM&quot;</code> if the time of day is before noon, <code dir="auto">&quot;PM&quot;</code> otherwise.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;h&quot;</code></td><td style="text-align:center">Hour in the <strong>12-hour</strong> format</td><td style="text-align:left">Lowercase <code dir="auto">&quot;h&quot;</code>. <br/>The included hour number from this token ranges from <code dir="auto">&quot;0&quot;</code> to <code dir="auto">&quot;11&quot;</code>. <br/>Use <code dir="auto">&quot;hh&quot;</code> for the two-digit hour with a leading zero for single-digit values.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;H&quot;</code></td><td style="text-align:center">Hour in the <strong>24-hour</strong> format</td><td style="text-align:left">Uppercase <code dir="auto">&quot;H&quot;</code>. <br/>The included hour number from this token ranges from <code dir="auto">&quot;0&quot;</code> to <code dir="auto">&quot;23&quot;</code>. <br/>Use <code dir="auto">&quot;HH&quot;</code> for the two-digit hour with a leading zero for single-digit values.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;m&quot;</code></td><td style="text-align:center">Minute</td><td style="text-align:left">Lowercase <code dir="auto">&quot;m&quot;</code> for the minute, not to be confused with <em>uppercase</em> <code dir="auto">&quot;M&quot;</code> for the month. <br/>Use <code dir="auto">&quot;mm&quot;</code> for the two-digit minute with a leading zero for single-digit values.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;s&quot;</code></td><td style="text-align:center">Second</td><td style="text-align:left">Lowercase <code dir="auto">&quot;s&quot;</code> for the second, not to be confused with <em>uppercase</em> <code dir="auto">&quot;S&quot;</code> for fractions of a second. <br/>Use <code dir="auto">&quot;ss&quot;</code> for the two-digit second with a leading zero for single-digit values.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;S&quot;</code></td><td style="text-align:center">Fractions of a second</td><td style="text-align:left">Uppercase <code dir="auto">&quot;S&quot;</code>. <br/> Includes the number of milliseconds in the fractional second (<code dir="auto">&quot;0&quot;</code> to <code dir="auto">&quot;999&quot;</code>). <br/> Use <code dir="auto">&quot;SS&quot;</code> or <code dir="auto">&quot;SSS&quot;</code> for the two-digit or three-digit millisecond number with leading zeros.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;Z&quot;</code></td><td style="text-align:center">Time zone (<strong>UTC offset</strong>)</td><td style="text-align:left">Uppercase <code dir="auto">&quot;Z&quot;</code>. <br/> Includes the hour and minute UTC offset value in <code dir="auto">&quot;HHmm&quot;</code> format, preceded by its sign (e.g., <code dir="auto">&quot;-0400&quot;</code>).</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;z&quot;</code></td><td style="text-align:center">Time zone (<strong>acronym or name</strong>)</td><td style="text-align:left">Lowercase <code dir="auto">&quot;z&quot;</code>. <br/> A single <code dir="auto">&quot;z&quot;</code> includes the abbreviation of the time zone (e.g., <code dir="auto">&quot;EDT&quot;</code>). <br/> Use <code dir="auto">&quot;zzzz&quot;</code> for the time zone’s name (e.g., <code dir="auto">&quot;Eastern Daylight Time&quot;</code>). <br/>It is not a placeholder for the <em>IANA identifier</em>. Use <a href="https://www.tradingview.com/pine-script-reference/v6/#var_syminfo.timezone">syminfo.timezone</a> to retrieve the exchange time zone’s IANA representation.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;:&quot;</code>, <code dir="auto">&quot;/&quot;</code>, <code dir="auto">&quot;-&quot;</code>, <code dir="auto">&quot;.&quot;</code>, <code dir="auto">&quot;,&quot;</code>, <code dir="auto">&quot;(&quot;</code>, <code dir="auto">&quot;)&quot;</code>, <code dir="auto">&quot; &quot;</code></td><td style="text-align:center">Separators</td><td style="text-align:left">These characters are separators for formatting tokens. <br/>They appear as they are in the formatted text. (e.g., <code dir="auto">&quot;01/01/24&quot;</code>, <code dir="auto">&quot;12:30:00&quot;</code>, <code dir="auto">&quot;Jan 1, 2024&quot;</code>). <br/> Some other characters can also act as separators. However, the ones listed are the most common.</td></tr><tr><td style="text-align:center"><code dir="auto">&quot;&#39;&quot;</code></td><td style="text-align:center">Escape character</td><td style="text-align:left">Characters enclosed within <em>two single quotes</em> appear as they are in the result, even if they otherwise act as formatting tokens. For example, <code dir="auto">&quot; &#39;Day&#39; &quot;</code> appears as-is in the resulting “string” instead of listing the day of the year, AM/PM postfix, and year.</td></tr></tbody></table> <p>The following example demonstrates how various formatting tokens affect the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function’s result. The script calls the function with different <code dir="auto">format</code> arguments to create date/time strings from <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time">time</a>, <a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a>, and <a href="https://www.tradingview.com/pine-script-reference/v6/#var_time_close">time_close</a> timestamps. It displays each <code dir="auto">format</code> value and the corresponding formatted result in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> on the last bar:</p> <p><img src="/pine-script-docs/_astro/Time-Formatting-dates-and-times-1.BsC0g9qz_29jz4f.webp" alt="image" width="1338" height="528" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Formatting dates and times demo&quot;, overlay = false) //@variable A `table` that displays different date/time `format` strings and their results. var table displayTable = table.new( position.middle_center, 2, 15, bgcolor = color.white, frame_color = color.black, frame_width = 1, border_width = 1 ) //@function Initializes a `displayTable` row showing a `formatString` and its formatted result for a specified // `timeValue` and `timezoneValue`. displayText(rowID, formatString, timeValue = time, timezoneValue = syminfo.timezone) =&gt; //@variable Is light blue if the `rowID` is even, white otherwise. Used to set alternating table row colors. color rowColor = rowID % 2 == 0 ? color.rgb(33, 149, 243, 75) : color.white // Display the `formatString` in the row&#39;s first cell. displayTable.cell( 0, rowID, formatString, text_color = color.black, text_halign = text.align_left, bgcolor = rowColor ) // Show the result of formatting the `timeValue` based on the `formatString` and `timezoneValue` in the second cell. displayTable.cell( 1, rowID, str.format_time(timeValue, formatString, timezoneValue), text_color = color.black, text_halign = text.align_right, bgcolor = rowColor ) if barstate.islast // Initialize the table&#39;s header cells. displayTable.cell(0, 0, &quot;FORMAT STRINGS&quot;) displayTable.cell(1, 0, &quot;FORMATTED DATE/TIME OUTPUT&quot;) // Initialize a row to show the default date-time &quot;string&quot; format and its result for `time`. displayTable.cell( 0, 1, &quot;(Default `str.format_time()` format)&quot;, text_color = color.black, text_halign = text.align_left, bgcolor = color.yellow) displayTable.cell( 1, 1, str.format_time(time), text_color = color.black, text_halign = text.align_right, bgcolor = color.yellow) // Initialize rows to show different formatting strings and their results for `time`, `time_close`, and `timenow`. displayText(2, &quot;dd/MM/yy&quot;) displayText(3, &quot;MMMM dd, yyyy&quot;) displayText(4, &quot;hh:mm:ss.SS a&quot;, timenow) displayText(5, &quot;HH:mm &#39;UTC&#39;Z&quot;) displayText(6, &quot;H:mm a (zzzz)&quot;) displayText(7, &quot;my day / &#39;my day&#39; (&#39;escaped&#39;)&quot;) displayText(8, &quot;&#39;Month&#39; M, &#39;Week&#39; w, &#39;Day&#39; DDD&quot;) displayText(9, &quot;&#39;Bar expected closing time&#39;: ha&quot;, time_close) displayText(10, &quot;&#39;Current date/time&#39;: MMM-d-y HH:mm:ss z&quot;, timenow) displayText(11, &quot;&#39;New Time zone&#39;: zzzz&quot;, timezoneValue = &quot;Australia/Sydney&quot;) displayText(12, &quot;&#39;Time zone change&#39;: MMM-d-y HH:mm:ss z&quot;, timenow, &quot;Australia/Sydney&quot;)</div> <h2 id="expressing-time-differences" class="md-heading"><a href="#expressing-time-differences">Expressing time differences<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h2> <p>Every <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamp</a> represents a specific point in time as the absolute <em>time difference</em> from a fixed historical point (epoch). The specific epoch all UNIX timestamps reference is <em>midnight UTC on January 1, 1970</em>. Programmers can <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">format</a> UNIX timestamps into readable date-time strings with the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function because it uses the time difference from the UNIX Epoch in its date and time calculations.</p> <p>In contrast, the difference between two nonzero UNIX timestamps represents the number of milliseconds elapsed from one absolute point to another. The difference does not directly refer to a specific point in UNIX time if neither timestamp in the operation has a value of 0 (corresponding to the UNIX Epoch).</p> <p>Programmers may want to express the millisecond difference between two UNIX timestamps in <em>other time units</em>, such as seconds, days, etc. Some might assume they can use the difference as the <code dir="auto">time</code> argument in a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> call to achieve this result. However, the function always treats its <code dir="auto">time</code> argument as the time elapsed from the <em>UNIX Epoch</em> to derive a <em>calendar date/time</em> representation in a specific <a href="/pine-script-docs/concepts/time/#time-zones">time zone</a>. It <strong>does not</strong> express time differences directly. Therefore, attempting to format timestamp <em>differences</em> rather than timestamps with <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> leads to unintended results.</p> <p>For example, the following script calculates the millisecond difference between the current execution time (<a href="https://www.tradingview.com/pine-script-reference/v6/#var_timenow">timenow</a>) and the “1M” bar’s closing time (<a href="https://www.tradingview.com/pine-script-reference/v6/#fun_time_close">time_close(“1M”)</a>) for a monthly countdown timer display. It attempts to express the time difference in another format using <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a>. It displays the function call’s result in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a>, along with the original millisecond difference (<code dir="auto">timeLeft</code>) and <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> date-time representations of the timestamps.</p> <p>As we see below, the table shows correct results for the formatted timestamps and the <code dir="auto">timeLeft</code> value. However, the formatted time difference appears as <code dir="auto">&quot;1970-01-12T16:47:10-0500&quot;</code>. Although the <code dir="auto">timeLeft</code> value is supposed to represent a difference between timestamps rather than a specific point in time, the <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format_time">str.format_time()</a> function still treats the value as a <strong>UNIX timestamp</strong>. Consequently, it creates a “string” expressing the value as a <em>date and time</em> in the UTC-5 time zone:</p> <p><img src="/pine-script-docs/_astro/Time-Relative-time-differences-1.Bn5sSvwp_ZWpy0r.webp" alt="image" width="1666" height="562" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Incorrectly formatting time difference demo&quot;, overlay = true) //@variable A table that displays monthly close countdown information. var table displayTable = table.new(position.top_right, 1, 4, color.rgb(0, 188, 212, 60)) if barstate.islast //@variable A UNIX timestamp representing the current time as of the script&#39;s latest execution. int currentTime = timenow //@variable A UNIX timestamp representing the expected closing time of the current &quot;1M&quot; bar. int monthCloseTime = time_close(&quot;1M&quot;) //@variable The number of milliseconds between the `currentTime` and the `monthCloseTime`. // This value is NOT intended as a UNIX timestamp. int timeLeft = monthCloseTime - currentTime //@variable A &quot;string&quot; representing the `timeLeft` as a date and time in the exchange time zone, in ISO 8601 format. // This format is INCORRECT for the `timeLeft` value because it&#39;s supposed to represent the time between // two nonzero UNIX timestamps, NOT a specific point in time. string incorrectTimeFormat = str.format_time(timeLeft) // Initialize `displayTable` cells to initialize the `currentTime` and `monthCloseTime`. displayTable.cell( 0, 0, &quot;Current time: &quot; + str.format_time(currentTime, &quot;HH:mm:ss.S dd/MM/yy (z)&quot;), text_size = size.large, text_halign = text.align_right ) displayTable.cell( 0, 1, &quot;`1M` Bar closing time: &quot; + str.format_time(monthCloseTime, &quot;HH:mm:ss.SS dd/MM/yy (z)&quot;), text_size = size.large, text_halign = text.align_right ) // Initialize a cell to display the `timeLeft` millisecond difference. displayTable.cell( 0, 2, &quot;`timeLeft` value: &quot; + str.tostring(timeLeft), text_size = size.large, bgcolor = color.yellow ) // Initialize a cell to display the `incorrectTimeFormat` representation. displayTable.cell( 0, 3, &quot;Time left (incorrect format): &quot; + incorrectTimeFormat, text_size = size.large, bgcolor = color.maroon, text_color = color.white )</div> <p>To express the difference between timestamps in other time units correctly, programmers must write code that <em>calculates</em> the number of units elapsed instead of erroneously formatting the difference as a specific date or time.</p> <p>The calculations required to express time differences depend on the chosen time units. The sections below explain how to express millisecond differences in <a href="/pine-script-docs/concepts/time/#weekly-and-smaller-units">weekly and smaller units</a>, and <a href="/pine-script-docs/concepts/time/#monthly-and-larger-units">monthly and larger units</a>.</p> <h3 id="weekly-and-smaller-units" class="md-heading"><a href="#weekly-and-smaller-units">Weekly and smaller units<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>Weeks and smaller time units (days, hours, minutes, seconds, and milliseconds) cover <em>consistent</em> blocks of time. These units have the following relationship:</p> <ul> <li>One week equals seven days.</li> <li>One day equals 24 hours.</li> <li>One hour equals 60 minutes.</li> <li>One minute equals 60 seconds.</li> <li>One second equals 1000 milliseconds.</li> </ul> <p>Using this relationship, programmers can define the span of these units by the number of <em>milliseconds</em> they contain. For example, since every hour has 60 minutes, every minute has 60 seconds, and every second has 1000 milliseconds, the number of milliseconds per hour is <code dir="auto">60 * 60 * 1000</code>, which equals <code dir="auto">3600000</code>.</p> <p>Programmers can use <em>modular arithmetic</em> based on the milliseconds in each unit to calculate the total number of weeks, days, and smaller spans covered by the difference between two <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a>. The process is as follows, starting from the <em>largest</em> time unit in the calculation:</p> <ol> <li>Calculate the number of milliseconds in the time unit.</li> <li>Divide the remaining millisecond difference by the calculated value and round down to the nearest whole number. The result represents the number of <em>complete</em> time units within the interval.</li> <li>Use the <em>remainder</em> from the division as the new remaining millisecond difference.</li> <li>Repeat steps 1-3 for each time unit in the calculation, in <em>descending</em> order based on size.</li> </ol> <p>The following script implements this process in a custom <code dir="auto">formatTimeSpan()</code> function. The function accepts two UNIX timestamps defining a start and end point, and its “bool” parameters control whether it calculates the number of weeks or smaller units covered by the time range. The function calculates the millisecond distance between the two timestamps. It then calculates the numbers of complete units covered by that distance and formats the results into a “string”.</p> <p>The script calls <code dir="auto">formatTimeSpan()</code> to express the difference between two separate <a href="/pine-script-docs/concepts/inputs/#time-input">time input</a> values in selected time units. It then displays the resulting “string” in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_table">table</a> alongside <a href="/pine-script-docs/concepts/time/#formatting-dates-and-times">formatted</a> representations of the start and end times:</p> <p><img src="/pine-script-docs/_astro/Time-Relative-time-differences-Weekly-and-smaller-units-1.CkIHycnQ_yxUkv.webp" alt="image" width="1256" height="412" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Calculating time span demo&quot;) // Assign the number of milliseconds in weekly and smaller units to &quot;const&quot; variables for convenience. const int ONE_WEEK = 604800000 const int ONE_DAY = 86400000 const int ONE_HOUR = 3600000 const int ONE_MINUTE = 60000 const int ONE_SECOND = 1000 //@variable A UNIX timestamp calculated from the user-input start date and time. int startTimeInput = input.time(timestamp(&quot;1 May 2022 00:00 -0400&quot;), &quot;Start date and time&quot;, group = &quot;Time between&quot;) //@variable A UNIX timestamp calculated from the user-input end date and time. int endTimeInput = input.time(timestamp(&quot;7 Sep 2024 20:37 -0400&quot;), &quot;End date and time&quot;, group = &quot;Time between&quot;) // Create &quot;bool&quot; inputs to toggle weeks, days, hours, minutes, seconds, and milliseconds in the calculation. bool weeksInput = input.bool(true, &quot;Weeks&quot;, group = &quot;Time units&quot;, inline = &quot;A&quot;) bool daysInput = input.bool(true, &quot;Days&quot;, group = &quot;Time units&quot;, inline = &quot;A&quot;) bool hoursInput = input.bool(true, &quot;Hours&quot;, group = &quot;Time units&quot;, inline = &quot;B&quot;) bool minutesInput = input.bool(true, &quot;Minutes&quot;, group = &quot;Time units&quot;, inline = &quot;B&quot;) bool secondsInput = input.bool(true, &quot;Seconds&quot;, group = &quot;Time units&quot;, inline = &quot;B&quot;) bool millisecondsInput = input.bool(true, &quot;Milliseconds&quot;, group = &quot;Time units&quot;, inline = &quot;B&quot;) //@function Calculates the difference between two UNIX timestamps as the number of complete time units in // descending order of size, formatting the results into a &quot;string&quot;. The &quot;int&quot; parameters accept timestamps, // and the &quot;bool&quot; parameters determine which units the function uses in its calculations. formatTimeSpan( int startTimestamp, int endTimestamp, bool calculateWeeks, bool calculateDays, bool calculateHours, bool calculateMinutes, bool calculateSeconds, bool calculateMilliseconds ) =&gt; //@variable The milliseconds between the `startTimestamp` and `endTimestamp`. int timeDifference = math.abs(endTimestamp - startTimestamp) //@variable A &quot;string&quot; representation of the interval in mixed time units. string formattedString = na // Calculate complete units within the interval for each toggled unit, reducing the `timeDifference` by those sizes. if calculateWeeks int totalWeeks = math.floor(timeDifference / ONE_WEEK) timeDifference %= ONE_WEEK formattedString += str.tostring(totalWeeks) + (totalWeeks == 1 ? &quot; week &quot; : &quot; weeks &quot;) if calculateDays int totalDays = math.floor(timeDifference / ONE_DAY) timeDifference %= ONE_DAY formattedString += str.tostring(totalDays) + (totalDays == 1 ? &quot; day &quot; : &quot; days &quot;) if calculateHours int totalHours = math.floor(timeDifference / ONE_HOUR) timeDifference %= ONE_HOUR formattedString += str.tostring(totalHours) + (totalHours == 1 ? &quot; hour &quot; : &quot; hours &quot;) if calculateMinutes int totalMinutes = math.floor(timeDifference / ONE_MINUTE) timeDifference %= ONE_MINUTE formattedString += str.tostring(totalMinutes) + (totalMinutes == 1 ? &quot; minute &quot; : &quot; minutes &quot;) if calculateSeconds int totalSeconds = math.floor(timeDifference / ONE_SECOND) timeDifference %= ONE_SECOND formattedString += str.tostring(totalSeconds) + (totalSeconds == 1 ? &quot; second &quot; : &quot; seconds &quot;) if calculateMilliseconds // `timeDifference` is in milliseconds already, so add it to the `formattedString` directly. formattedString += str.tostring(timeDifference) + (timeDifference == 1 ? &quot; millisecond&quot; : &quot; milliseconds&quot;) // Return the `formattedString`. formattedString if barstate.islastconfirmedhistory //@variable A table that that displays formatted start and end times and their custom-formatted time difference. var table displayTable = table.new(position.middle_center, 1, 2, color.aqua) //@variable A &quot;string&quot; containing formatted `startTimeInput` and `endTimeInput` values. string timeText = &quot;Start date and time: &quot; + str.format_time(startTimeInput, &quot;dd/MM/yy HH:mm:ss (z)&quot;) + &quot;\n End date and time: &quot; + str.format_time(endTimeInput, &quot;dd/MM/yy HH:mm:ss (z)&quot;) //@variable A &quot;string&quot; representing the span between `startTimeInput` and `endTimeInput` in mixed time units. string userTimeSpan = formatTimeSpan( startTimeInput, endTimeInput, weeksInput, daysInput, hoursInput, minutesInput, secondsInput, millisecondsInput ) // Display the `timeText` in the table. displayTable.cell(0, 0, timeText, text_color = color.white, text_size = size.large, text_halign = text.align_left) // Display the `userTimeSpan` in the table. displayTable.cell(0, 1, &quot;Time span: &quot; + userTimeSpan, text_color = color.white, text_size = size.large, text_halign = text.align_left, bgcolor = color.navy)</div> <p>Note that:</p> <ul> <li>The <a href="/pine-script-docs/language/user-defined-functions/">user-defined function</a> uses <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_math.floor">math.floor()</a> to round each divided result down to the nearest “int” value to get the number of <em>complete</em> units in the interval. After division, it uses the modulo assignment operator (<a href="https://www.tradingview.com/pine-script-reference/v6/#op_%25=">%=</a>) to get the <em>remainder</em> and assign that value to the <code dir="auto">timeDifference</code> variable. This process repeats for each selected unit.</li> </ul> <p>The image above shows the calculated time difference in mixed time units. By toggling the “bool” inputs, users can also isolate specific units in the calculation. For example, this image shows the result after enabling only the “Milliseconds” input:</p> <p><img src="/pine-script-docs/_astro/Time-Relative-time-differences-Weekly-and-smaller-units-2.CwvuIXRj_ZdIqWW.webp" alt="image" width="988" height="338" loading="lazy" decoding="async"></p> <h3 id="monthly-and-larger-units" class="md-heading"><a href="#monthly-and-larger-units">Monthly and larger units<span class="icon icon-link" aria-hidden="true"><svg width="28" height="28" viewBox="0 0 28 28"><g fill="currentColor" clip-path="url(#a)"><path d="M14.908 5.558a5.326 5.326 0 1 1 7.533 7.533l-3.236 3.236-1.061-1.061 3.236-3.236a3.826 3.826 0 1 0-5.411-5.411l-3.236 3.236-1.06-1.06 3.235-3.237ZM5.56 14.907a5.326 5.326 0 0 0 7.532 7.533l3.236-3.236-1.061-1.061-3.236 3.236a3.826 3.826 0 1 1-5.411-5.411l3.236-3.236-1.061-1.06-3.236 3.235Z"></path><path d="m16.346 10.592-5.753 5.753 1.061 1.06 5.753-5.752-1.06-1.06Z"></path></g><defs><clippath id="a"><path fill="#fff" d="M0 0h28v28H0z"></path></clippath></defs></svg></span></a></h3> <p>Unlike weeks and smaller units, months and larger units <em>vary</em> in length based on calendar rules. For example, a month can contain 28, 29, 30, or 31 days, and a year can contain 365 or 366 days.</p> <p>Some programmers prefer to use the modular arithmetic outlined in the <a href="/pine-script-docs/concepts/time/#weekly-and-smaller-units">previous section</a>, with <em>approximate lengths</em> for these irregular units, to calculate large-unit durations between <a href="/pine-script-docs/concepts/time/#unix-timestamps">UNIX timestamps</a>. With this process, programmers usually define the units in either of the following ways:</p> <ul> <li>Using <em>common</em> lengths, e.g., a common year equals 365 days, and a common month equals 30 days.</li> <li>Using the <em>average</em> lengths, e.g., an average year equals 365.25 days, and an average month equals 30.4375 days.</li> </ul> <p>Calculations involving approximate units produce <em>rough estimates</em> of the elapsed time. Such estimates are often practical when expressing relatively short durations. However, their precision diminishes with the size of the difference, drifting further away from the actual time elapsed.</p> <p>Therefore, expressing time differences in monthly and larger units with precision requires a different calculation than the process outlined above. For a more precise estimate of months, years, and larger units elapsed, the calculations should use the <em>actual</em> span of each individual unit rather than approximations, meaning it must account for <em>leap years</em> and <em>variations</em> in month sizes.</p> <p>The advanced example below contains a custom <code dir="auto">formatTimeDifference()</code> function that calculates the years and months, in addition to days and smaller units, elapsed between two UNIX timestamps.</p> <p>The function uses the process outlined in the <a href="/pine-script-docs/concepts/time/#weekly-and-smaller-units">previous section</a> to calculate the daily and smaller units within the interval. For the monthly and yearly units, which have <em>irregular</em> lengths, the function uses a <a href="/pine-script-docs/language/loops/#while-loops">while</a> loop to iterate across calendar months. On each iteration, it increments monthly and yearly counters and subtracts the number of days in the added month from the day counter. After the loop ends, the function adjusts the year, month, and day counters to account for partial months elapsed between the timestamps. Finally, it uses the counters in a <a href="https://www.tradingview.com/pine-script-reference/v6/#fun_str.format">str.format()</a> call to create a formatted “string” containing the calculated values.</p> <p>The script calls this <code dir="auto">formatTimeDifference()</code> function to calculate the years, months, days, hours, minutes, seconds, and milliseconds elapsed between two separate <a href="/pine-script-docs/concepts/inputs/#time-input">time input</a> values and displays the result in a <a href="https://www.tradingview.com/pine-script-reference/v6/#type_label">label</a>:</p> <p><img src="/pine-script-docs/_astro/Time-Relative-time-differences-Monthly-and-larger-units-1.BhYyGmym_1QIbUd.webp" alt="image" width="1060" height="428" loading="lazy" decoding="async"></p> <div class="pine-colorizer not-content">//@version=6 indicator(&quot;Calculating time span for larger units demo&quot;) //@variable The starting date and time of the time span, input by the user. int startTimeInput = input.time(timestamp(&quot;3 Apr 2022 20:00 -0400&quot;), &quot;Start date&quot;, group = &quot;Time between&quot;) //@variable The ending date and time of the time span, input by the user. int endTimeInput = input.time(timestamp(&quot;3 Sep 2024 15:45 -0400&quot;), &quot;End date&quot;, group = &quot;Time between&quot;) //@function Returns the number of days in the `monthNumber` month of the `yearNumber` year. daysPerMonth(int yearNumber, int monthNumber) =&gt; //@variable Is `true` if the `yearNumber` represents a leap year. bool leapYear = (yearNumber % 4 == 0 and yearNumber % 100 != 0) or (yearNumber % 400 == 0) //@variable The number of days calculated for the month. int result = switch monthNumber == 2 =&gt; leapYear ? 29 : 28 =&gt; 31 - (monthNumber - 1) % 7 % 2 //@function Calculates the relative time difference between two timestamps, covering monthly and larger units. formatTimeDifference(int timestamp1, int timestamp2) =&gt; // The starting time and ending time. int startTime = math.min(timestamp1, timestamp2), int endTime = math.max(timestamp1, timestamp2) // The year, month, and day of the `startTime` and `endTime`. int startYear = year(startTime), int startMonth = month(startTime), int startDay = dayofmonth(startTime) int endYear = year(endTime), int endMonth = month(endTime), int endDay = dayofmonth(endTime) // Calculate the total number of days, hours, minutes, seconds, and milliseconds in the interval. int milliseconds = endTime - startTime int days = math.floor(milliseconds / 86400000), milliseconds %= 86400000 int hours = math.floor(milliseconds / 3600000), milliseconds %= 3600000 int minutes = math.floor(milliseconds / 60000), milliseconds %= 60000 int seconds = math.floor(milliseconds / 1000), milliseconds %= 1000 // Calculate the number of days in the `startMonth` and `endMonth`. int daysInStartMonth = daysPerMonth(startYear, startMonth), int daysInEndMonth = daysPerMonth(endYear, endMonth) //@variable The number of days remaining in the `startMonth`. int remainingInMonth = daysInStartMonth - startDay + 1 // Subtract `remainingInMonth` from the `days`, and offset the `startDay` and `startMonth`. days -= remainingInMonth, startDay := 1, startMonth += 1 // Set `startMonth` to 1, and increase the `startYear` if the `startMonth` exceeds 12. if startMonth &gt; 12 startMonth := 1, startYear += 1 // Initialize variables to count the total number of months and years in the interval. int months = 0, int years = 0 // Loop to increment `months` and `years` values based on the `days`. while days &gt; 0 //@variable The number of days in the current `startMonth`. int daysInMonth = daysPerMonth(startYear, startMonth) // Break the loop if the number of remaining days is less than the `daysInMonth`. if days &lt; daysInMonth break // Reduce the `days` by the `daysInMonth` and increment the `months`. days -= daysInMonth, months += 1 // Increase the `years` and reset the `months` to 0 when `months` is 12. if months == 12 months := 0, years += 1 // Increase the `startMonth` and adjust the `startMonth` and `startYear` if its value exceeds 12. startMonth += 1 if startMonth &gt; 12 startMonth := 1, startYear += 1 // Re-add the `remainingInMonth` value to the number of `days`. Adjust the `days`, `months`, and `years` if the // new value exceeds the `daysInStartMonth` or `daysInEndMonth`, depending on the `startDay`. days += remainingInMonth if days &gt;= (startDay &lt; daysInStartMonth / 2 ? daysInStartMonth : daysInEndMonth) months += 1 if months == 12 months := 0, years += 1 days -= remainingInMonth // Format the calculated values into a &quot;string&quot; and return the result. str.format( &quot; Years:   {0}\n Months: {1}\n Days:    {2}\n Hours:    {3}\n Minutes:  {4}\n Seconds: {5} \n Milliseconds: {6}&quot;, years, months, days, hours, minutes, seconds, milliseconds ) if barstate.islastconfirmedhistory //@variable A &quot;string&quot; representing the time between the `startTimeInput` and `endTimeInput` in mixed units. string userTimeSpan = formatTimeDifference(startTimeInput, endTimeInput) //@variable Text shown in the label. string labelText = &quot;Start time: &quot; + str.format_time(startTimeInput, &quot;dd/MM/yy HH:mm:ss (z)&quot;) + &quot;\n End time: &quot; + str.format_time(endTimeInput, &quot;dd/MM/yy HH:mm:ss (z)&quot;) + &quot;\n---------\nTime difference:\n&quot; + userTimeSpan label.new( bar_index, high, labelText, color = #FF946E, size = size.large, textalign = text.align_left, style = label.style_label_center )</div> <p>Note that:</p> <ul> <li>The script determines the number of days in each month with the user-defined <code dir="auto">daysPerMonth()</code> function. The function identifies whether a month has 28, 29, 30, or 31 days based on its month number and the year it belongs to. Its calculation accounts for leap years. A leap year occurs when the year is divisible by 4 or 400 but not by 100.</li> <li>Before the <a href="/pine-script-docs/language/loops/#while-loops">while</a> loop, the function subtracts the number of days in a partial starting month from the initial day count, aligning the counters with the beginning of a new month. It re-adds the subtracted days after the loop to adjust the counters for partial months. It adjusts the month and year counters based on the days in the <code dir="auto">startMonth</code> if the <code dir="auto">startDay</code> is less than halfway through that month. Otherwise, it adjusts the values based on the days in the <code dir="auto">endMonth</code>.</li> </ul> </div> <div class="pagination-buttons not-content" data-astro-cid-xgirumru> <a href="/pine-script-docs/concepts/text-and-shapes" class="pagination-card" data-pagefind-ignore data-astro-cid-assl6cvf> <p data-astro-cid-assl6cvf>Previous</p> <h4 class="pagination-card-header" data-astro-cid-assl6cvf> <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-assl6cvf data-icon="theme/arrow-back"> <use xlink:href="#ai:local:theme/arrow-back"></use> </svg> Text and shapes </h4> </a> <a href="/pine-script-docs/concepts/timeframes" class="pagination-card" data-pagefind-ignore data-astro-cid-assl6cvf> <p data-astro-cid-assl6cvf>Next</p> <h4 class="pagination-card-header" data-astro-cid-assl6cvf> Timeframes <svg width="28" height="28" viewBox="0 0 28 28" data-astro-cid-assl6cvf data-icon="theme/arrow"> <symbol id="ai:local:theme/arrow"><path fill="none" stroke="var(--arrow-fill-color, #131722)" d="m11 8 6 6-6 6"/></symbol><use xlink:href="#ai:local:theme/arrow"></use> </svg> </h4> </a> </div> </main> <div id="toc" data-pagefind-ignore="all" data-astro-cid-oor6cujd><aside class="document-toc-container ml-4 w-48" data-astro-cid-oor6cujd><section id="toc-scroll-section" class="slick-scroll" data-astro-cid-oor6cujd><header data-astro-cid-oor6cujd><h2 class="toc-header" data-astro-cid-oor6cujd>On this page</h2></header><ul class="" id="toc-entries" data-astro-cid-oor6cujd><a href="#introduction" aria-current="true" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-current data-astro-cid-oor6cujd>Introduction</li></a><a href="#unix-timestamps" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>UNIX timestamps</li></a><a href="#time-zones" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Time zones</li></a><a href="#time-zone-strings" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Time zone strings</li></a><a href="#time-variables" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Time variables</li></a><a href="#time-and-time_close-variables" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96time&#96 and &#96time_close&#96 variables</li></a><a href="#time_tradingday" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96time_tradingday&#96</li></a><a href="#timenow" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96timenow&#96</li></a><a href="#calendar-based-variables" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Calendar-based variables</li></a><a href="#last_bar_time" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96last_bar_time&#96</li></a><a href="#visible-bar-times" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Visible bar times</li></a><a href="#syminfotimezone" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96syminfo.timezone&#96</li></a><a href="#time-functions" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Time functions</li></a><a href="#time-and-time_close-functions" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96time()&#96 and &#96time_close()&#96 functions</li></a><a href="#testing-for-sessions" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-4" data-astro-cid-oor6cujd>Testing for sessions</li></a><a href="#testing-for-changes-in-higher-timeframes" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-4" data-astro-cid-oor6cujd>Testing for changes in higher timeframes</li></a><a href="#calendar-based-functions" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Calendar-based functions</li></a><a href="#timestamp" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>&#96timestamp()&#96</li></a><a href="#formatting-dates-and-times" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Formatting dates and times</li></a><a href="#expressing-time-differences" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-2" data-astro-cid-oor6cujd>Expressing time differences</li></a><a href="#weekly-and-smaller-units" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Weekly and smaller units</li></a><a href="#monthly-and-larger-units" class="document-toc-link" data-astro-cid-oor6cujd><li class="l-3" data-astro-cid-oor6cujd>Monthly and larger units</li></a></ul></section><div class="toc-bottom-fade" data-astro-cid-oor6cujd></div><div class="back-top-space" data-astro-cid-oor6cujd><div class="not-content" style="" data-astro-cid-pkzv2hgs> <a id="back-top-button" title="Back to top" href="#top" data-astro-cid-pkzv2hgs class="floating-button not-content stvb-base stvb-pointer stvb-gray stvb-medium stvb-secondary stvb-icon stvb-icon-force-color stvb-force-no-border"> <svg width="18" height="18" viewBox="0 0 18 18" data-astro-cid-oor6cujd data-icon="theme/arrow-up"> <symbol id="ai:local:theme/arrow-up"><g fill="none"><g clip-path="url(#a)"><path fill="currentColor" fill-rule="evenodd" d="m9 3.5.5.44 4 3.5-1 1.12-2.75-2.4V14h-1.5V6.15L5.49 8.56l-.98-1.12 4-3.5L9 3.5Z" clip-rule="evenodd"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h18v18H0z"/></clipPath></defs></g></symbol><use xlink:href="#ai:local:theme/arrow-up"></use> </svg> </a> </div> </div></aside></div> </main> </div> <footer data-astro-cid-b6pf7ola> <div class="footer-content" data-astro-cid-b6pf7ola> <ul data-astro-cid-b6pf7ola> <li data-astro-cid-b6pf7ola> <a href="https://www.tradingview.com/chat/#BfmVowG1TZkKO235" data-astro-cid-b6pf7ola>Pine Q&amp;A chat</a> </li><li data-astro-cid-b6pf7ola> <a href="https://stackoverflow.com/questions/tagged/pine-script" data-astro-cid-b6pf7ola>Stack Overflow</a> ↗ </li><li data-astro-cid-b6pf7ola> <a href="https://t.me/PineCodersQA" data-astro-cid-b6pf7ola>Telegram</a> ↗ </li><li data-astro-cid-b6pf7ola> <a href="https://www.reddit.com/r/TradingView/" data-astro-cid-b6pf7ola>Reddit</a> ↗ </li> </ul> <div class="flex" data-astro-cid-b6pf7ola></div> <div class="copyright" data-astro-cid-b6pf7ola>Copyright © 2024 TradingView, Inc.</div> </div> </footer> </body></html>

Pages: 1 2 3 4 5 6 7 8 9 10