Unable to transform DateNum() to a string

Hello,

I am unable to transform Datenum() to a string, I can't figure out what I'm doing wrong, I should appreciate some help from the community. Please, consider the following example:

// In the first exploration I create the following static variable

StaticVarSet("DateVariable", DateNum());

// In the second exploration I call the previous static variable for some calculations

ReferenceDate = StaticVarGet("DateVariable");

// After using ReferenceDate for some calculations, I need to show the date in a column of the exploration report

DateFromReferenceDate = DateTimeConvert(2, DateNum());

Filter = 1;
AddTextColumn(DateFromReferenceDate, "Date from DateNum()");

I know that DateTimeConvert is not the right formula since it will return a number or an array and I would need a string, as Date(), but I have been unable to find the correct way to do it.

Thanks for your help.

Regards.

@jptrader in your last line you are using an array of datetime so you should change it to:

AddColumn(DateFromReferenceDate, "Date from DateNum()", formatDateTime);

using the specific format required to properly display datetime values.
(Check the AddColumn() reference if you prefer to see your dates in the ISO datetime format).

For other purposes, to convert a single datetime (not an array) to a string you could use either the function DateTimeToStr() or the more flexible DateTimeFormat().

Hello beppe,

Thanks for your prompt answer, now AddColumn does provide the date in the right format. However, a little issue remains...

Filter = 1;
SetOption("NoDefaultColumns", True);
AddTextColumn(Date(), "Date 1");
AddColumn(DateNum(), "Date 2", formatDateTime);

Capture

Date() is providing the date corresponding to the last bar, while DateNum() is providing a previous one. How is that happening?

Regards.

'formatDateTime' is used for 'DateTime()'

Filter = 1;
SetOption("NoDefaultColumns", True);
AddTextColumn(Date(), "Date 1");
AddColumn(DateNum(), "Date 2", 1);
AddColumn(DateTime(), "Date 3", formatDateTime);

Hello TrendSurfer,

Thanks for your clarification, I made a mess with the functions.

Filter = 1;
SetOption("NoDefaultColumns", True);
AddTextColumn(Date(), "Date 1");
DateFromReferenceDate = DateTimeConvert(2, DateNum());
AddColumn(DateFromReferenceDate, "Date 2", formatDateTime);

Capture

Regards.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.