Explore output YYYY/MM/DD HH:MM:SS?

I've read article DateTimeToStr() output YYYY/MM/DD HH:MM:SS?,
it use GetCursorXPosition() to get datetime of selected bar.

Q) How to do this for every bar for Explore (if I don't setup datetime format on my operating system)? my code below get some error, please help, Thank you :smiley:!

yyyymmdd = DateTimeToStr(DateTime(), mode=4); // 4 - only YYYY-MM-DD => Error.5
hhmmss = DateTimeToStr(DateTime(), mode=5); // 5 - only HH:MM:SS => Error.5
dt = yyyymmdd + hhmmss;
AddColumn( dt, "date/time", formatDateTime);
  1. yyyymmdd and hhmmss of your code are type string but AddColumn expects array or number as input. For string output use AddTextColumn
  2. DateTimeToStr first argument expects number but not array. Error 5 clearly tells you so. DateTime() is array function.

To output selected value or last value (both being type number):

SetOption("NoDefaultcolumns", True);

dt_val = SelectedValue(DateTime());
dt_string = DateTimeFormat("%Y/%m/%d %H:%M:%S", dt_val);
AddTextColumn(Name(), "Ticker", 1);
AddTextColumn( dt_string, "date/time", 1);
AddColumn( C, "Close", 1.2);
Filter = Status("lastbarinrange");

Otherwise if you want to get custom date format for entire array then change OS (operating system) settings and use two lines code below.

AddColumn( C, "Close", 1.2);
Filter = 1;

Or without changing system settings... use AddRow() . But then you would have to iterate (to output with custom date format on entire array). Note: you won't have cell color formatting option for AddRow output.

The question is why would you want to do this (just for changing date format)?

1 Like

The reason of why do I want to do this
is to test the other way to output yyyy/mm/dd hh:mm:ss.

After your instructions,
I know to change OS settings is the only good way (currently)
to show date and time for every day.

Thank you :smiley:!

You can easily get ISO date format YYYY-MM-DD HH:MM:SS without changing system locale settings:

AddColumn( DateTime(), "DT", formatDateTimeISO ); // gives YYYY-MM-DD HH:MM:SS
Filter = 1;

The slashes / are not used by ISO date format, therefore if you really want them instead of standard dashes - you need to change your OS regional settings to use the date format you want using this code:

AddColumn( DateTime(), "DT", formatDateTime ); // would output according to regional settings
Filter = 1;

The problem with completely customizable date formats is that while it is easy to output anything, you are going to get issues with parsing them (converting back to datetime number) and/or sorting of date columns outputted in non-standard formats because many times given sequence of numbers can be interpreted in a number of ways. For example: 01/02/03 - can be interpreted in 6 different ways.