How to format systemtime?

D.D.K-2637 966 Reputation points
2022-03-01T02:57:57.26+00:00

Hi,
I'm trying to use std::format to format a string for systemtime (e.g. 1/3/2022)

SYSTEMTIME t;  
const auto today=format("today is: {}", t);  

178695-image.png

however, when compiled i get an error:

178696-image.png

Please show me how to make it work.
I searched for std::format examples with datetime but didn't find it.

Many thansk!

Developer technologies C++
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-03-01T06:10:40.463+00:00

    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  
    

    178718-result.png

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.