问题
I want to create a date-time picker control that shows both date and time, as a combination of the DTS_SHORTDATECENTURYFORMAT
and DTS_TIMEFORMAT
styles. Since there is no such style built into the date-time picker, I have to do it myself with GetLocaleInfoEx()
.
I notice that by default, GetLocaleInfoEx(LOCALE_SSHORTDATE)
seems to return a four-digit year, which is what I want. Is this reliable? It's not documented anywhere, only that there can be more than one string for LOCALE_SSHORTDATE
. If I have to enumerate the possible strings, how would I know which one is the one I want? Do I just look for the first string to contain yyyy
? That doesn't sound reliable to me, at least not on its own...
Thanks.
回答1:
No you can't rely on LOCALE_SSHORTDATE
having a four-digit year, since the user is able to configure this through the Region control panel and options with two-digit years are available.
If you want to enforce a four digit year (and override the user's preference) you may as well just provide your own short date string. Alternatively, you could scan the user's string and change "yy" to "yyyy".
回答2:
You can also use GetDateFormat
and GetTimeFormat
in combination with LOCALE_SSHORTDATE
etc.
wchar_t buf[100];
SYSTEMTIME st;
GetSystemTime(&st);
GetDateFormat(0, DATE_LONGDATE, &st, 0, buf, 100);
GetDateFormat(0, DATE_SHORTDATE, &st, 0, buf, 100);
GetTimeFormat(0, 0, &st, 0, buf, 100);
GetTimeFormat(0, TIME_NOSECONDS, &st, 0, buf, 100);
Then maybe you don't have to worry about how the formatting is done. Year is sometimes set to yy
even for long date format. For example in ControlPanel->Region->Format: switch to French language, it allows different options than US English.
来源:https://stackoverflow.com/questions/30632211/what-is-the-correct-way-to-get-a-locale-sshortdate-that-is-guaranteed-to-have-a