C ++ What's the correct use of LCID in COleDateTime for American date
I need to parse an American date string to extract the date.
COleDateTime dData;
LCID lcid = 0x409; // 0x409 is the locale ID for English US
CString strDate;
dData.ParseDateTime("10/1/2014 9:43:00 AM", VAR_DATEVALUEONLY);
strDate = dData.Format(0, lcid);
I expect this to return on October 1, 2014, but instead, it will return on October 10, 2014.
Can someone please tell me what I am doing wrong here?
source to share
I assume you are getting January 10, 2014, not October 10, 2014. Apart from your parsing without providing an lcid argument (and use instead LANG_USER_DEFAULT
- see the other answer), the parsing code is using VarDateFromStr
which, in turn, presumably does not perform complex pattern matching and instead just asks for a LOCALE_IDATE
value for the locale.
The value 1 (day-month-year) you have causes this order of values.
LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT); // 0x409 is the locale ID for English US
TCHAR pszIDate[32] = { 0 };
ATLVERIFY(GetLocaleInfo(lcid, LOCALE_IDATE, pszIDate, _countof(pszIDate)) > 0);
_tprintf(_T("LOCALE_IDATE: %s\n"), pszIDate);
COleDateTime dData;
dData.ParseDateTime(_T("10/1/2014 9:43:00 AM"), VAR_DATEVALUEONLY, lcid); // LANG_USER_DEFAULT
_tprintf(_T("%s\n"), dData.Format(0, lcid));
dData.m_dt -= 1.0;
_tprintf(_T("%s\n"), dData.Format(0, lcid));
From (see setting "Short date"):
You get:
LOCALE_IDATE: 1 10-Jan-14 09-Jan-14
and
You get:
LOCALE_IDATE: 0 10/1/2014 9/30/2014
I suppose you should avoid parsing date and time strings using this deprecated API unless you just formatted the argument string from a value on the same system.
source to share