Open Date Validator Project

Let’s start by saying I’m a crappy programmer. Always have been, always will be (so no complaints about what has already been clearly established).

Anyway, I have been having trouble getting ParseDate to produce consistent results on various computers because of the whole regional date thing, so I wrote a simple date validator tonight that works for the next 100 years in my particular circumstance. Just pass it a date in the ‘d/m/yyyy’ format (no leading zeroes) and it will return true or false if the date is valid.

I would love to create a date validator that can be passed various date formats and still validate the date correctly (but unfortunately, I have to move on to the next challenge in my program).

I’m hoping someone can help add those features to create a better routine than this one or at least create a faster version of the routine.

So, please open a beer and outdo me (and Parsedate)!

Sub ValidDate as Boolean
dim thisDate as new date

dim thisMonth as integer
dim thisDay as integer
dim thisYear as integer
dim x as integer

dim dateArray(36500) as string

for x = 0 to 36500
thisMonth = thisDate.month
thisDay = thisDate.day
thisYear = thisDate.year

dateArray(x) = cstr(thisMonth) + "/" + cstr(thisDay) + "/" + cstr(thisYear)

if dateArray(x) = sDate then return true

thisDate.day = thisDate.day + 1

next x
return false
End Sub

I don’t understand… You seem to be providing sDate in the format of M/D/Y and ParseDate loves that format (in the US at least), as ParseDate per the LangRef :

[quote]
ParseDate supports the date formats specified by the user’s regional/international system settings. If Text is a date in another format, then ParseDate will return False[/quote]

So IF your system date format is NOT M/D/Y then ParseDate will seem to fail on certain date strings

  • 1/2/16 is Jan 2,2016 for the US format, but is Feb 1,2016 for other formats
  • 31/12/16 is an ERROR for US formats, but is Dec 31,2016 for other formats

your program simply needs to be aware of the locale to get the date right

So it looks like you are supplying sDate in a format that does NOT match the locale of the computer, and thus interpets dates incorrectly, and your “solution” does 36,500 calculations attempting to circumvent this

A lot depends on “Where did sDate come from?”

Scratching head… what do you want to accomplish with you code?

For any code one saves the date internally in an neutral format (SQL date or similar) and then one presents this date to the user in the current locale.

Any date you get from an outside source (like the mbox data that my app parses) you need to have some assumptions about the locale or the format of the data.

In the original code I had to work with, I have a string date in a field in the m/d/yyyy format, so I don’t have control over the code that originates the date–just the string that I’m given to work with.

When I use ParseDate on the string on Mac and Windows, it works completely fine, but when I used ParseDate on Ubuntu, it failed each time. I know that you can set the date format on the Linux computer to get a ParseDate result you like, but I won’t have access to the end user’s computer. I just needed something that could verify that the date wasn’t February 30th or June 31st for example.

If you KNOW you are going to get the date in M/D/Y format then do something like

dim v() as string
dim dt as new date
dim m as integer
dim d as integer
dim y as integer
dim flag as boolean=true // assume its valid
v=split(sDate,"/")
if v.ubound<>2 then flag=false // not enough or too many "parts"
//
m=val(v(0))
d=val(v(1))
y=val(v(2))
if y<2000 then y=y+2000 // you may need to adjust this
//
if m>=1 and m<=12 and d>=1 and d<=31 then 
//
dy.month=m
dy.day=d
dy.year=y
// now d has internally recalculated
flag=(d.month=m) and (d.day=d) and (d.year=y)
else
flag=false // month or day out of range
end if

This too is a bit brute force, but no where near doing 36,000 + calculations for each date

Yep, that’s what I meant! I needed something that would work consistently but much more efficiently than the ten year array.

So does my idea work for? If so, click “this answers my question”
If not, give me some information as to why it doesn’t

I did. Thanks for your help!