我需要得到系统当前日期格式字符串像( “DD-MM-YYYY”, “MM/DD/YYYY” 等。获取系统日期格式字符串的Win32
GetDateFormat()API返回的格式化字符串如 “2015年12月9日”,但需要字符串,如 “DD-MM-YYYY”
C#解决方案
string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
但我需要在Win32中。
我需要得到系统当前日期格式字符串像( “DD-MM-YYYY”, “MM/DD/YYYY” 等。获取系统日期格式字符串的Win32
GetDateFormat()API返回的格式化字符串如 “2015年12月9日”,但需要字符串,如 “DD-MM-YYYY”
C#解决方案
string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
但我需要在Win32中。
你可以格式字符串列表目前适用,列举它们s通过EnumDateFormats完成。请注意,可以(通常是)多于一个,因此您必须决定选择哪一个1)。
下面的代码返回系统默认的日期格式短模式:
static std::list<std::wstring> g_DateFormats;
BOOL CALLBACK EnumDateFormatsProc(_In_ LPWSTR lpDateFormatString) {
// Store each format in the global list of dateformats.
g_DateFormats.push_back(lpDateFormatString);
return TRUE;
}
std::wstring GetShortDatePattern() {
if (g_DateFormats.size() == 0 &&
// Enumerate all system default short dateformats; EnumDateFormatsProc is
// called for each dateformat.
!::EnumDateFormatsW(EnumDateFormatsProc,
LOCALE_SYSTEM_DEFAULT,
DATE_SHORTDATE)) {
throw std::runtime_error("EnumDateFormatsW");
}
// There can be more than one short date format. Arbitrarily pick the first one:
return g_DateFormats.front();
}
int main() {
const std::wstring strShortFormat = GetShortDatePattern();
return 0;
}
示例代码:
//#include <time.h>
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
//The years are since 1900 according the documentation, so add 1900 to the actual year result.
char cDate[255] = {};
sprintf(cDate, "Today is: %d-%d-%d", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year + 1900);
这不能解决问题。问题是,如何获得当前选择的**格式字符串**,而不是如何使用任意格式的字符串来构造日期时间的字符串表示形式。 – IInspectable
外表在注册表中,'HKEY_CURRENT_USER \控制面板\ International'。您可能需要sShortDate' – wimh
永远不要在注册表中查找。有一个真正的API:['GetLocaleInfoEx()'](https://msdn.microsoft.com/en-us/library/windows/desktop/dd318103%28v=vs.85%29.aspx)。 – andlabs
@andlabs,请你详细告诉它。 – Anil8753