date validation in classic asp
i am new in classic asp and having problem in validating the date
dim Day,Month,Year,FullDate
Day = "01"
Month = "20"
Year = "2012"
FullDate = Month + "/" + Day + "/" + Year
document.write FullDate
document.write IsDate(FullDate)
document.write IsDate(CDate(FullDate))
document.write IsDate(20/01/2012)
output :
20/01/2012
true true false
If you’re asking why
document.write IsDate(20/01/2012)doesn’t writetruethe reason is because you’ve asked the computer to do division, then evaluate that as a date.20/01 = 20 => 20/2012 ~= 0.01IsDate(0.01) => falseIf you really want to test what you’ve got try this instead (small tweak)
Also, just for clarification http://en.wikipedia.org/wiki/Date_format_by_country
Some countries use
and some places use
and that’s why the International Standards Organization suggests you do things with least specificity to most specificity:
Notice that’s Years -> Months (which month is more specific than which year) -> Days (which day an event occurs on is helpful) -> Hours (don’t be late!) -> Minutes (saved by the bell?) -> Seconds (Now you have some idea when it happened) -> fractions of a second (Olympic Swimming!!)
Years are rather non-specific. Lots of things happen in one year. So those should always parse first. The ISO way is the preferred way to pass Date information, and when the year does not come first, the system tries to guess intelligently. Since some parts of the world do
dmyand some domdyand since only one of your starting two numbers is over 12, it assumes you meandmyinstead ofmdy. No WTF here.For the record, here are a list of countries which predominantly put the month first as a matter of tradition in
mdyformat (excluding ISO formatting which is not tradition, but science)And finally if you want to write a function that will try and reparse the date for you:
Consider that people tend to break the date with either spaces, periods, hyphens or slashes, they may write it as “20120817” or they may include the time as well. There may be a T in the middle, and it may have a Z at the end.
Sample inputs: (and the date they represent)
As you can see, there’s a fair bit of parsing that has to happen here, and that’s to assume that they have a 10 digit string and that that 10 digits is a date. Here are some other date formats:
And then you have to parse
So as you can see, parsing the function seems easy but there’s a lot to consider, and this is JUST dates. Want to let’s talk about times next?