CDate function using different locales

孤者浪人 提交于 2019-12-11 13:11:59

问题


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.

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/14141272/cdate-function-using-different-locales

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!