Hi @D.D.K-2637 ,
The reason for this error is that SYSTEMTIME
is a structure and cannot be used directly in format
, so you need to convert SYSTEMTIME
to string
, the sample code is as follows:
(In order to run the following code you need to refer to the method in this link to turn off the 4996 warning
. )
#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <format>
int main()
{
SYSTEMTIME st, lt;
GetSystemTime(&st);
std::string strMessage;
CString cstrMessage;
cstrMessage.Format(_T("%d-%02d-%02d %02d:%02d:%02d.%03d"),
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond,
st.wMilliseconds);
strMessage = CT2A(cstrMessage.GetString());
std::cout << "System time = " << strMessage << std::endl;
std::ostringstream ossMessage;
ossMessage << st.wYear << "-"
<< std::setw(2) << std::setfill('0') << st.wMonth << "-"
<< std::setw(2) << std::setfill('0') << st.wDay << " "
<< std::setw(2) << std::setfill('0') << st.wHour << ":"
<< std::setw(2) << std::setfill('0') << st.wMinute << ":"
<< std::setw(2) << std::setfill('0') << st.wSecond << "."
<< std::setw(3) << std::setfill('0') << st.wMilliseconds;
strMessage = ossMessage.str();
std::cout << "System time = " << strMessage << std::endl;
char buffer[256];
sprintf(buffer,
"%d-%02d-%02d %02d:%02d:%02d.%03d",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond,
st.wMilliseconds);
strMessage = buffer;
std::cout << "System time = " << strMessage << std::endl;
const auto today = std::format("today is: {}", strMessage);
return 0;
}
You could also use the members in SYSTEMTIME one by one, for example:
const auto today = format("today is: {}/{}/{}", t.wDay,t.wMonth,t.wYear);//e.g. 1/3/2022
Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.