Friday, November 30, 2007

Electro Negatively Table

ASP - how to check if a year is a leap

In questo post presenterò il codice ASP che ci permette di verificare se un anno è bisestile o no.

Lo script è composto da una funzione principale, cuore del calcolo, e da un "blocco if" con il quale si intercetta la risposta.

E' utile fare in questo modo ( e non con una sola semplice funzione) per poter verificare anche se sono avvenuti errori in the calculation.

The code is as follows:


\u0026lt;% Function
annobisestile (data_num)
on error resume next
if not (isempty (data_num) or isnull (data_num) or len (trim (data_num)) = 0) then 'if there is a parameter
if err \u0026lt;> 0 then annobisestile = vbNull
if IsNumeric (data_num) then' if it's a year if
data_num \u0026lt;> 0 then 'if it is different from zero
year = abs ( data_num) 'bad years are considered positive
end if elseif
IsDate (data_num) then' if it's a date
year = year (data_num) 'extrapolates year
else' if it is neither a date nor a year
annobisestile = vbNull

end if if err \u0026lt;> 0 then annobisestile = vbNull

'/***** ************************************************** ****************************/
'/ One year is a leap year if it is divisible by 4 but not by 100. But it is a leap year * /
'/ if it is divisible by 400 * /
'/********************************************* **************************************/

'check if
is a leap year if (( (year mod 4 = 0) and (year mod 100 \u0026lt;> 0)) or (year mod 400 = 0)) = true then
annobisestile
else
annobisestile = false

end if if err \u0026lt;> 0 then annobisestile = vbNull
else 'if there is no parameter
annobisestile = vbNull
end if end function


strtest = 2007
if annobisestile (strtest)
= true then 'code here if you want the year is a leap
annobisestile elseif (strtest) = vbNull
then 'here the code that you want if there was an error
' or was not passed an incorrect parameter

else 'here if you want the code that is not a leap year


end if%>


If you are sure to pass a parameter to the function correctly, ie a number corresponding to one year (greater than 0 and positive) or a date in a correct format then you can simplify the "if block" in this way:


strtest = 2007
if annobisestile (strtest) then
'here the code that you want if the year is a leap

else' here the code you want is not a leap year if end if



I hope I was clear! If you want to download the script this is the link.
In the script of the Zip is not the second if block, that simple.

If you have any questions, criticisms or suggestions or simply want clarification do not hesitate to contact me

soon!


Updated:

one shown above is an "educational function": D. I update this post to put much less complex than another that does the same thing. The function is this:


\u0026lt;% function
annobisestile (year)
dim miadata
if IsDate (year) then
miadata = "28/02 /" & year (year)
else
miadata = "28/02 /" & year end if

miadata = dateadd ("d", 1, miadata)
if day (miadata) = 29 then
annobisestile = true else

annobisestile
= false end if end function

%>

Note however that in the latter function have not put a check errors then you should be sure to spend a year as a value or a date!
The rest is the same. The function returns the Boolean value true if the year is a leap year or false if not. In

zippo lynched above is NOT included it running!