visual-c++mfccoledatetime

Parsing CString YYYYMMDD into COleDateTime to get SHORTDATE?


I have a date in a CString: "20240415". How can I parse this into a COleDateTime? My goal is to format this as a short date.

I tried:

COleDateTime lastInstalledDate;
if (lastInstalledDate.ParseDateTime(strInstallDate, VAR_DATEVALUEONLY, LANG_USER_DEFAULT))
{
    strInstallDate = lastInstalledDate.Format(VAR_DATEVALUEONLY);
}

But the result was still: "20240415" instead of "15/04/2024".


Solution

  • I'd transform L"20240415" into L"2024-04-15" somehow, for example by inserting 2 hyphens and then simply use COleDateTime::Format:

        CString strInstallDate = L"20240415";
    
        strInstallDate.Insert(4, L'-'); strInstallDate.Insert(7, L'-');
        COleDateTime lastInstalledDate;
        lastInstalledDate.ParseDateTime(strInstallDate, VAR_DATEVALUEONLY);
        CString strFormattedInstallDate = lastInstalledDate.Format(VAR_DATEVALUEONLY);
        ...