C++ Builder: Date string is not converting correctly

236 views Asked by At

I am using Embarcadero C++ Builder Alexandria 11.1.

The frmMain form has a TDatePicker dtpDate used to select a date.

The selected date (TDate) value is converted to the "yyyy-mm-dd" format.

I need to convert the selected date value to a char* for the following function:

int fnc_ExtractYear(char* yyyymmdd)
{
    ...
}

This function is from a DLL.

I am using the c_str() function to convert the "2022-07-10" value to char*, but I am getting the following value:

0x0000000003623480 "2022-07-10"

void __fastcall TfrmMain::btnExecuteClick(TObject *Sender)
{
    int year;
    TDate date;
    AnsiString strdate;
    const char* cdate;

    switch(rdgSelect->ItemIndex)
        {
        case  0 :
            date = dtpDate->Date; // Date (:TDate) = 10/07/2022
            // strDate (:AnsiString) = "2022-07-10"
            strdate = FormatDateTime("yyyy-mm-dd", date); 
            // cdate = 0x0000000003623480 "2022-07-10" 
            cdate = strdate.c_str();
            year = fnc_ExtractYear(cdate);
            lblResult->Caption = IntToStr(year);
            break;
        ...
        }

The converted value to char* does not work.

The int fnc_ExtractYear(char* yyyymmdd) function returns -1 as the year value instead of 2022 when I pass the 0x0000000003623480 "2022-07-10" value.

I need to get the converted value to char* as "2022-07-10" and not to 0x0000000003623480 "2022-07-10".

How can I solve this problem?

1

There are 1 answers

1
pjackson On

The RTL has many functions that allow you to work with dates and strings. One easy way to get just the end of the date string would be to use AnsiRightStr():

year = fnc_ExtractYear(AnsiRightStr(cdate, 10));