Prabhu
Prabhu

Reputation: 717

How can I convert Double Date to std::string

I got the Date value from the method from the following code

  DATE dDate;
  hr = pADsUser->get_PasswordLastChanged(&dDate);
  //   pADsUser is pointer variable of   IADsUser

Date type is discribed in the link http://msdn.microsoft.com/en-us/library/82ab7w69%28v=vs.71%29.aspx

How can I convert this kind of Date to string so that I can print it in console.

I am not using MFC Dlls for this application. so I cannot use the COleDateTime type also.

Is there any built in method available or Do I need to calculate the date manually?

Upvotes: 1

Views: 2308

Answers (3)

Yogesh Chaudhari
Yogesh Chaudhari

Reputation: 1

TCHAR *strDate = new TCHAR[12];
tm * ptm = gmtime ( &ttAppClosing );
int day = ptm->tm_mday;
int month = ptm->tm_mon+1;
int year = ptm->tm_year;

CString sDay = L"";
sDay.Format(L"%d", day);
if(day < 9)
    sDay.Format(L"0%d", day);

CString sMonth = L"";
sMonth.Format(L"%d", month);
if(month < 9)
    sMonth.Format(L"0%d", month);

_stprintf(strDate, L"%s-%s-%d", sDay, sMonth, 1900 + year);

CString sCloseDate(strDate);

Upvotes: 0

l33t
l33t

Reputation: 19966

Why not use the underlying API then? Linking with Boost can be costly if you're counting bytes :P

DATE d;
CComBSTR bstr;
VarBstrFromDate(d, lcid, dwFlags, &bstr);

Upvotes: -1

moshbear
moshbear

Reputation: 3322

Look into Boost.Lexical_cast for conversion to and from string.

You will have to write a converter function that unpacks the DATE type into a (date, time) tuple so that lexical_cast won't convert from double.

According to the doc, it looks like the date type is (day - 1899-12-30) . (time - 0).

Using boost::date_time, you can create a time object with date-time (1899-12-30,0), then increment days(abs(DATE)) and hours((DATE - abs(DATE)) * 24).

Upvotes: 5

Related Questions