1. Home
  2. Functions
  3. How to Get Current Date and Time in thinkScript (Examples)

How to Get Current Date and Time in thinkScript (Examples)

In this article, you will learn to get the current date and time in thinkScript.

The following functions will be utilized throughout the post.

  • GetYYYYMMDD()
  • SecondsFromTime()
  • SecondsTillTime()

Furthermore, because of thinkScript’s versatility, you can obtain the date and time from a specific period. We will cover that in this article as well.

thinkScript GetYYYYMMDD()

When dealing with date, we must use the GetYYYYMMDD() function.

This function returns the date of the current bar. And the format it uses is Year/Month/Day.

Use the following snippet to display the current date as a label on your ThinkorSwim chart.

def date = GetYYYYMMDD();
AddLabel(yes, AsPrice(date), color.green);

Because the default formatting is not easy to read, we need to customize it.


Current date in 00/00/0000 Format

# GetYYYYMMDD() Broken Down to 00/00/0000 Format
# Mobius

def data = getYYYYMMDD();
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);
addLabel(1, "date: " + month + "/" + day + "/" + AsPrice(year), color.white);

Much better, right? Credit to Mobius from the thinkScript community for the code.


Get date from a specific period

What if we want to plot something from a specific point in time rather than the current date?

It’s definitely possible. You might remember our article on the Anchored VWAP indicator. It makes use of the same idea.

Here’s how to get a specific date in thinkScript.

input pastDate = 20220505;
def priceAtDate = if GetYYYYMMDD() == pastDate then low else Double.NaN;
plot marker = priceAtDate;
marker.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

The script above draws an up arrow on May 5th, 2022.

You can change the custom date from the indicator settings.


Get the date five bars back

#From OneNote drive
declare lower;
input LookBack = 5;
def start = !IsNan(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
def end = IsNaN(close[-1]);
rec DateAtStart = if Start then getYyyyMmDd() else DateAtStart[1];
plot DateAtLookBack = DateAtStart;

Current time in ThinkorSwim

You can check the current time in ThinkorSwim from the top menu.

As far as thinkScript goes, there are two time-related functions.

  • SecondsFromTime()
  • SecondsTillTime()

Let’s get to know them.

thinkScript SecondsFromTime()

The SecondsFromTime() function returns the number of seconds from the defined time.

declare lower;
def AftMktOpen = secondsFromTime(930) > 0;
plot data = if AftMktOpen then 1 else 0;

The script above plots 1 if the current time is later than 9:30 AM (EST).


thinkScript SecondsTillTime()

SecondsTillTime() returns the number of seconds till the specified time.

AddVerticalLine(secondstilltime(1145)==0, "Lunch Start", color.red, curve.firm);
AddVerticalLine(secondstilltime(1345)==0, "Lunch End", color.red, curve.firm);

From 11:45 am – 1:30 pm is lunch time in New York.

The example above utilized the SecondsTillTime() function to make an indicator that reminds us of this schedule.

It plots a vertical line at 11:45 am and another one at 1:30 pm (EST).

Unlock the Power of ThinkorSwim

Get the latest news and updates on TD Ameritrade, ThinkorSwim indicators, thinkScript tutorials, and trading strategies delivered straight to your inbox every week.

We don’t spam! Unsubscribe at anytime.

One thought on “How to Get Current Date and Time in thinkScript (Examples)

  1. Can you explain why this doesn’t move into the future rather than into the past:
    “close[-LookBack + 1])”? I thought that minus index references meant into the future and positive ones meant referencing past data. I’ve seen this sort of reference in some of the thinkorSwim studies but never could understand what the references were doing. Thanks.
    ***********************************************************************************************
    -+Get the date five bars back
    declare lower;
    input LookBack = 5;
    def start = !IsNan(close[-LookBack + 1]) and IsNaN(close[-LookBack]);
    def end = IsNaN(close[-1]);
    rec DateAtStart = if Start then getYyyyMmDd() else DateAtStart[1];
    plot DateAtLookBack = DateAtStart;

Leave a Reply

Your email address will not be published. Required fields are marked *