将 CString YYYYMMDD 解析为 COleDateTime 以获得 SHORTDATE?

问题描述 投票:0回答:1

我在

CString
有个约会:
"20240415"
。我怎样才能将其解析为
COleDateTime
?我的目标是将其格式化为短日期。

我尝试过:

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

但结果仍然是:

"20240415"
而不是
"15/04/2024"

visual-c++ mfc coledatetime
1个回答
0
投票

这有效:

CString strInstallDate = L"20240415";

const auto longInstallDate = _ttol(strInstallDate);
const auto nYear = longInstallDate / 10000;
const auto nMonth = (longInstallDate / 100) % 100;
const auto nDay = longInstallDate % 100;

COleDateTime datInstallDate;
datInstallDate.SetDateTime(nYear, nMonth, nDay, 0, 0, 0);

CString strFormattedInstallDate = datInstallDate.Format(VAR_DATEVALUEONLY);
© www.soinside.com 2019 - 2024. All rights reserved.