StrToDateTime requires VALID string representing date

Tomasz, I have the following code

MergeDate = GetFnData("WebID");
Merged = IIf(MergeDate != "", DateTimeDiff(DateTime_, StrToDateTime(MergeDate))>= 0, 0);

I get the following error message:

![Screenshot 2025-12-21 190314|556x500](upload://eDVcS7BXK5lo4ZpX52L9Z0eeQDC.png)

Please re-post, I don't see any screenshot.

Please display the actual value that you are passing, like this:

_TRACEF("'%s'", MergeDate );

The error will be displayed if you pass a value that has invalid format too, for example if you pass "abcd", it will display the same error message.

The only string that is accepted by StrToDateTime is VALID date string (such as for example "2012-05-12").

The only "invalid" string that is silently accepted by 7.00.1 is EMPTY string and it produces NULL value:

StrToDateTime(""); // will silently accept empty string and return Null

Invalid strings like "abcd" that don't represent a valid date will result in the error message.

See the following code:

// the first example is an empty string
_N(examples = ",1911-02-01,1970-12-01,2025-02-03");

for( i = 0; i < 4; i++ )
{
   str = StrExtract( examples, i );
   
   dt = StrToDateTime( str );
   
   printf("String '%s', IsNull %g, datetime: '%s'\n", str, IsNull( dt ), DateTimeToStr( dt ) );
} 
1 Like

Tomasz, the culprit was that for two symbols, WebID Field had a value of “ “ (two spaces). The previous version didn’t complain. I like the fact 7.00.1 is stricter. Thanks again.

I have modified following code, giving error only when dividend pay date field is empty

For help use SUPPORT@amibroker.com not announcement threads.

Your code is incorrect.

GetFnData("DividendPayDate") DOES NOT RETURN STRING!

GetFnData("DividendPayDate") returns a NUMBER representing date-time field.
Your code is wrong. You are passing date/time number to StrToDateTime that expects STRING as input. You don't need to call StrToDateTime at all since the input data is already date/time. Plus you really need to check for Nulls. Correct usage is as follows:

divdate = GetFnData("DividendPayDate");

if( IsNull( divdate ) ) 
{
  Title = "No Dividend Date Is Set";
}
else
{
  secondsInDay = 24 * 60 * 60;
  diff_in_days = DateTimeDiff( Now(5), divdate )/secondsInDay;
  Title = StrFormat( "DivDate = %s, diff in days %g", DateTimeToStr(divdate ), diff_in_days ); 
}

1 Like

Summary:

  1. StrToDateTime requires VALID string representing date, preferably in ISO format YYYY-MM-DD or (with time) YYYY-MM-DD HH:mm:ss

  2. The only "invalid" string that it accepts silently is "" (empty string). In such case it will silently return Null

  3. Date String must represent date > 1911-01-01

  4. Any other input (invalid input string, only spaces instead of empty string, date not in supported range) will result in an ERROR message.

The error message does NOT mean error in AmiBroker, but IN YOUR FORMULA. You need to verify what you are passing to StrToDateTime. In doubt use

the_string = ....
_TRACEF("The value that is passed is: '%s'", the_string );
output = StrToDateTime( the_string );
3 Likes

Thank you for your detailed response. I wasn’t referring to that particular error in AmiBroker — it had been functioning correctly prior to version 7.0, which is why several codes required modification. No issue though, the code has now been successfully updated.

No it wasn’t functioning. The fact the error in parameters gets unnoticed doesn’t mean it is functioning. It just means it gets unnoticed and you are not aware of the formula error. Again it is an error in the formula, not in AmiBroker. AmiBroker reports formula errors. Stricter parameter checking is there for a reason, to make you aware of errors in the formula, so code can truly work correctly.

Like dictionary reports spelling errors in the text you are writing,

1 Like