3

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.

andlabs
  • 11,290
  • 1
  • 31
  • 52

2 Answers2

3

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".

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • In that case I wonder why `DTS_SHORTDATECENTURYFORMAT` is present at all... I may wind up just keeping what I currently have (take whatever `GetLocaleInfoEx()` returns for the default short date string), if that is the best of the options presented here. Thanks in the meantime. – andlabs Jun 04 '15 at 00:38
  • I think I'm going to go with the changing `yy` to `yyyy` option as this seems to be what the standard control does for `DTS_SHORTDATECENTURYFORMAT` :/ Better to imitate and be consistent with the rest of the system, I suppose. Thanks. – andlabs Jun 05 '15 at 18:40
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.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77