1

I am using QTP (therefore VBScript) and I seem to have a problem with the CDate function.

When my colleague in Malaysia is using it there is not a problem. When I use it here in Spain I have the following error.

enter image description here

Now all I am asking is if there are any known issues with this function or any other related ones like FormatDateTime between different locales.

Anybody knows this?

Thanks in advance.

Pixie
  • 412
  • 1
  • 8
  • 26
  • Have a look at with my CDateLocale solution: https://stackoverflow.com/questions/67859593/how-can-i-set-the-locale-so-that-cdate-does-the-correct-conversion/67859594#67859594 – Robert F. Lamping Jun 06 '21 at 13:25
  • This post is from a a long time ago, but the kind of question I see around much and was not answered. Have a look at: [how-can-i-set-the-locale-so-that-cdate-does-the-correct-conversion](https://stackoverflow.com/questions/67859593/how-can-i-set-the-locale-so-that-cdate-does-the-correct-conversion/67859594#67859594) – Robert F. Lamping Jun 06 '21 at 13:28

2 Answers2

4

The error specifies the problem. todateObject.GetROProperty("value") contains a value that CDate does not recognize as a date.

You will need to first determine what is being returned by todateObject.GetROProperty("value"), and then make sure it can be converted to a date.

You may want to consider creating the date using DateSerial instead of CDate to avoid problems arising from cultural differences. If that is the case, depending on your circumstance you may need to provide separate fields for Day, Month, and Year to assure the order is always correct.

Daniel
  • 12,982
  • 3
  • 36
  • 60
  • Thanks Daniel for your quick answer. Could it be then that because I read using the value 2 in FormatDateTime it usies the regional setting of every country? Should I change that as well? – Pixie Jan 03 '13 at 15:41
  • @pixie I don't know. The problem with your code based upon the error message though, is CDate. I don't know what values you are passing, but likely Ekkehard's question relates to your specific issue. His answer is after all, better than mine. – Daniel Jan 03 '13 at 15:51
  • +1 because getting (and validating) the date parts separately and building the date via DateSerial is a *sure* strategy, while my method will work under specific conditions only. – Ekkehard.Horner Jan 03 '13 at 16:04
  • I thought the same think... Yours is a better approach but have to recode a lot ;) vision VS inmediate solution. thanks guys! – Pixie Jan 03 '13 at 16:19
4

CDate() tries to convert its input to a date, taking the Locale Setting into account. See:

>> SetLocale "en-us"
>> WScript.Echo GetLocale()
>> WScript.Echo 1, CDate("1 dec 2011")
>> WScript.Echo 2, CDate("1 dez 2011")
>>
1033
1 01.12.2011
Error Number:       13
Error Description:  Type mismatch
>> SetLocale "de"
>> WScript.Echo GetLocale()
>> WScript.Echo 3, CDate("1 dez 2011")
>> WScript.Echo 4, CDate("1 dec 2011")
>>
1031
3 01.12.2011
Error Number:       13
Error Description:  Type mismatch
>>

CDate() under "en-us" understands "dec", but chokes on "dez"; "de" works with "dez", but not with "dec".

A minimal impact strategy to solve your problem could be to enforce 'one locale for all' by using SetLocale() and some big stick (input validation) against sloppy data entry persons.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96