0

The amibroker function datenum() returns an array with dates represented in numbers. How to convert this array into the string equivalent?

DateNum

I have this function below which almost accomplishes this task except that only year 2000 and after is supported. How to fix it such that dates before year 2000 can be supported?

/*
Function changes DateNum ex:1040928 en String ddmmyyyy ex:28/09/2004 ( only > 2000 year )
*/
function sDate( nDate )  
{

    string = StrFormat( "%0.9g", nDate );

    //extract string part
    aa = StrLeft( string, 3 );
    mm = StrMid( string, 3, 2 );
    dd = StrRight( string, 2 );

    //transform year en num
    aa1 = StrToNum( aa ) + 1900; // ONLY CORRECT AFTER 2000
    yyyy = NumToStr( aa1, 1, False );

    result = yyyy + "-" + mm + "-" + dd;
    return result;
}
guagay_wk
  • 26,337
  • 54
  • 186
  • 295

2 Answers2

1

Change line

string = StrFormat( "%0.9g", nDate );

to

string = StrFormat( "%07.07g", nDate );
fxshrat
  • 89
  • 5
0

You can convert datenum to string easily using built-in afl functions, this is the code to use:

DateNumberArray = DateTimeConvert( 2, DateNum() );

for ( i = 0; i < BarCount; i++ )
{
_TRACE( DateTimeToStr( DateNumberArray [i] ) );
}

2 is a code for converting from DateNum, see DateTimeConvert documentation.