Format COleDatetime

Flaviu_ 1,031 Reputation points
2022-12-22T18:03:39.997+00:00

How can I format a COleDateTime valid object like Windows Explorer looks like ?
273428-image.png

https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/coledatetime-class?view=msvc-170#format

Is not what I need, because doesn't format the date time like in Windows Explorer ... can you help me ? I didn't find anything on internet ...

I know if user choose another format to the date time from windows settings, my COleDateTime format should track that change ...

Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2022-12-22T23:03:44.697+00:00

    Give this a try -

    COleDateTime dt = COleDateTime::GetCurrentTime();  
      
    SYSTEMTIME st{};  
    dt.GetAsSystemTime(st);  
      
    CString strDate, strTime, strDateTime;  
      
    int cchDate = ::GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, NULL, 0);  
    LPTSTR pDate = strDate.GetBufferSetLength(cchDate);  
    cchDate = ::GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, pDate, cchDate);  
    strDate.ReleaseBuffer();  
      
    int cchTime = ::GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, NULL, 0);  
    LPTSTR pTime = strTime.GetBufferSetLength(cchTime);  
    cchTime = ::GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, pTime, cchTime);  
    strTime.ReleaseBuffer();  
      
    strDateTime.Format(_T("%s %s"), (LPCTSTR) strDate, (LPCTSTR) strTime);  
    _tprintf_s(_T("%s\n"), (LPCTSTR)strDateTime);  
      
    
    0 comments No comments

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.