0

I have these lines

SetLocale(3081)
Response.Write "<p>TEST: " & Date() & " | " & isDate("3/22/2014") & " --> " & GetLocale() & "</p>"

which outputs

TEST: 3/07/2014 | True --> 3081

now correct me if i'm wrong but isn't there only 12 months in a year?, according to IsDate the date i've passed, which should be wrong because i have put in 22 as the month, is valid despite the local settings saying otherwise.

i want to validate the date to be the correct format to insert into the database and if it isn't give a more friendly error, "3/22/2014" will output "Error converting data type varchar to date." when i try to inster it into the database because it's getting by the IsDate check

What have i done wrong here?

Memor-X
  • 2,870
  • 6
  • 33
  • 57
  • Can you try using `Session.LCID = 3081` instead of `setLocale` ? – Flakes Jul 03 '14 at 06:38
  • @SearchAndResQ done that, no difference, `GetLocale()`/`Session.LCID` return whatever the i set using `Session.LCID`/`SetLocale(#)` so the `response.write` confirms the locale setting and the format `Date()` is coming out as – Memor-X Jul 03 '14 at 06:43
  • (http://stackoverflow.com/questions/4338025/isdate-function-returns-unexpected-results)[Please check if you can use anything from this thread] – Flakes Jul 03 '14 at 06:55
  • @SearchAndResQ: You've got to be really careful when assuming that the script version of any VB command is the same as the equivalent VB6, VBA or VB.NET command - basically most of them aren't. As a fail safe for dates I always use the `dd-mmm-yyyy hh:mm:ss` format (obviously excluding the time if necessary). – Paul Jul 03 '14 at 08:17

1 Answers1

2

Yes, this is indeed a valid date.

Why? Because VBScript is smart/generous/stupid (choose your favorite) enough to treat numbers as dates. And "3/22/2014" can be parsed as formula: 3 / 22 / 2014 = 6.770786313983931e-5

Now take this number and convert to date:

Dim myNumber, myDate
myNumber = 6.770786313983931e-5
myDate = CDate(myNumber)

The variable myDate will be perfectly valid date, which is December 30th 1899, 00:00:06

So bottom line: the value is a date, just not what you expect. You've done nothing wrong, but to really check if a string is valid date you will have to check it yourself, no out of the box methods.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208