Ad

How To Get Date From CDBVariant?

i am trying to get the the day ,month ,year from sql server using CDBVariant but i cant able to do that my code is below

what i want is just cout the day ,month ,year in my own format like this

3 jan 1990

in my student table i use date type not the date/time so i want to just cout the date in my own format because the date i get from database is year-month-date i want to show it as date-month-year

I cant able to convert CDBVariant::m_pdate to ctime

    CDBVariant date; 
    rs.GetFieldValue(short(2),date);//

    CTime a(date.m_pdate->year, date.m_pdate->month, date.m_pdate->day);

    cout << " date is:" << a.GetYear() << endl;
    a=NULL; //so the next value can be insert into ctime                
    rs.MoveNext(); // move to next tuple in table
}

i am making CTime a NULL so the next value can be insert becuase ctime show first time ok but after that it start adding on it dont know why all i want is just get the date from database and cout it.

Ad

Answer

The proper way to convert CDBVariant to COleDateTime is the following:

CDBVariant var;

if(var.m_dwType == DBVT_DATE) 
{
    COleDateTime timestamp(var.m_pdate->year,var.m_pdate->month,var.m_pdate->day,
                     var.m_pdate->hour,var.m_pdate->minute,var.m_pdate->second);
    CString str = timestamp.Format(_T("%B %d, %Y"));
}
Ad
source: stackoverflow.com
Ad