Just found GetDateFormatA
is outdated, you should use GetDateFormatEx
Simple example:
#include <windows.h>
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
const int len = 50;
LPWSTR date = new WCHAR[len];
GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_AUTOLAYOUT, NULL, NULL, date, len, NULL);
LPWSTR time = new WCHAR[len];
GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, NULL, NULL, time, len );
std::wcout << date << std::endl << time;
return 0;
}
If you have question about the api parameters, you may find GetDateFormatEx and GetTimeFormatEx in msdn are useful.
For _setmode(_fileno(stdout), _O_U16TEXT);
, it's a hack to print unicode under windows console.
You may refer to this answer
Note WinApis above depend on long time format, not short time format.
Though I couldn't find api for short time format. If you want a time format no including seconds, use TIME_NOSECONDS
instead of LOCALE_NAME_USER_DEFAULT
.