locked
Converting between Date and BSTR with custom date locale RRS feed

  • Question

  • Hi,

    Can someone please explain to me why my conversion from BSTR Variant to date Variant using custom local user date format is not working while conversion from date to BSTR works fine?

    To setup the custom local user date format, I went to Control Panel ->Regions and Languages ->Formats tab -> Additional Settings -> Date tab, then changed the short date format to ddMMMyyyy. 

    Here is my test code:

    // quick and dirty initialization

    double dblDate = 42325.750671; CComVariant vSrc; CComVariant vDestBstr; CComVariant vDestDate; vSrc.ChangeType(VT_DATE); vSrc.date = dblDate; // convert from date to bstr VariantChangeTypeEx(&vDestBstr, &vSrc, GetUserDefaultLCID(), 0, VT_BSTR); printf("%S\n", vDestBstr.bstrVal); // convert from bstr obtained above back to date HRESULT hr = VariantChangeTypeEx(&vDestDate, &vDestBstr, GetUserDefaultLCID(), 0, VT_DATE); if (FAILED(hr)) printf(" failed: 0x%x\n", hr); printf("%f\n", vDestDate.date);

    So the first converson from Date to BSTR using GetUserDefaultLCID() worked fine and printed "17Nov2015 6:00:58 PM" (in ddMMMyyyy format), but converting the same BSTR back to date failed, with hr = 0x80020005 (type mismatch) and vDestDate of 0.0.

    Can someone suggest how I can coerce a BSTR in custom date format back to Date Variant?

    Thanks!


    • Edited by jzfang Wednesday, November 18, 2015 8:21 AM
    Wednesday, November 18, 2015 8:20 AM

Answers

  • Hi, jzfang,

         In terms of your situation, does it make a different to set your date type to a similar but valid format?

    like “dd/MMM/yyyy”, “dd MMM yyyy and etc.

         

    Best regards,

    chchlll

    • Marked as answer by jzfang Thursday, November 26, 2015 11:05 PM
    Thursday, November 19, 2015 9:32 AM
  • On 11/22/2015 6:38 PM, jzfang wrote:

    Just a follow on question, is there something in the windows API that will return the configured date format string

    GetLocaleInfo[Ex](LOCALE_SSHORTDATE)

    • Marked as answer by jzfang Thursday, November 26, 2015 11:05 PM
    Monday, November 23, 2015 1:14 AM

All replies

  • Hi, jzfang,

         In terms of your situation, does it make a different to set your date type to a similar but valid format?

    like “dd/MMM/yyyy”, “dd MMM yyyy and etc.

         

    Best regards,

    chchlll

    • Marked as answer by jzfang Thursday, November 26, 2015 11:05 PM
    Thursday, November 19, 2015 9:32 AM
  • What value is returned from GetUserDefaultLCID()?

    In my case, the function returns an LCID of 1033 and there are no errors processing the date/BSTR conversions.

    Thursday, November 19, 2015 5:43 PM
  • Hi chchlll,

    Following your suggestion, I tried "dd/MMM/yyyy" and "dd MMM yyyy". Both of these date format worked at converting string to date. So it does look like VariantChangeTypeEx()  needs delimiter between fields to transform string back to dates.

    Just a follow on question, is there something in the windows API that will return the configured date format string (i.e. return the string "ddMMMyyyy") so I can write my own string to date converter as a last resort?

    Thanks.

    James

    Sunday, November 22, 2015 11:38 PM
  • Hi RLWA32,

    For me GetUserDefaultLCID() returns 3081, English(Australia). However, I'm guessing the issue in my test scenario here is that I'm overriding the default date format for LCID 3081 (d/MM/yyyy) by applying my own custom date format "ddMMMyyyy" over it.

    As chchlll suggested, it seems VariantChangeTypeEx() has trouble converting a date string to date if the current date format has no delimiter between the fields.

    Regards,

    James

    Sunday, November 22, 2015 11:45 PM
  • For me GetUserDefaultLCID() returns 3081, English(Australia). However, I'm guessing the issue in my test scenario here is that I'm overriding the default date format for LCID 3081 (d/MM/yyyy) by applying my own custom date format "ddMMMyyyy" over it.
    I asked about the LCID value because the code did not result in a date string that omitted delimiters.  The variant conversion functions use the LCID to determine the formatting of the returned date string.  I think you can determine the format that the conversion functions will use by using GetLocaleInfoEx
    Monday, November 23, 2015 12:34 AM
  • On 11/22/2015 6:38 PM, jzfang wrote:

    Just a follow on question, is there something in the windows API that will return the configured date format string

    GetLocaleInfo[Ex](LOCALE_SSHORTDATE)

    • Marked as answer by jzfang Thursday, November 26, 2015 11:05 PM
    Monday, November 23, 2015 1:14 AM