difference in year

# Thread: difference in year

1. Senior Member
Join Date
Dec 1969
Posts
234

## difference in year

Hi i want the difference in years.<BR>so i used this code below but the DateDiff always shows 0.<BR>Is there any other way to find the difference between the two years??<BR><BR>A = session("strEmpFromYear"&i)<BR>B = session("strEmpToYear"&i)<BR> <BR>DiffADate = "Diff in Years: " & DateDiff("yyyy", B, A)<BR>Response.Write DiffADate<BR><BR>Any help is much appreciated.

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## It would help...

...if you ALSO showed what values you are getting for A and B.<BR><BR>&#060;%<BR>A = session("strEmpFromYear"&i)<BR>B = session("strEmpToYear"&i)<BR><BR>Response.Write "DEBUG: dates " & A & " and " & B _<BR> & "have Diff in Years: " & DateDiff("yyyy", B, A) & "&#060;HR&#062;"<BR>%&#062;<BR><BR>I suspect you will find that you aren&#039;t even getting valid values for A and B.<BR><BR>I&#039;m a little leery of you using so many Session variables. You&#039;d be better off using an array, almost surely.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
234

## shows Zero

still it shows 0!<BR><BR>DEBUG: dates 2000 and 2001have Diff in Years: 0

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Because 2000 and 2001 ...

...are *NOT* dates!!!!!<BR><BR>They are *ALREADY* years!<BR><BR>A DATE *ALWAYS* must consist of year, month, and day. Period.<BR><BR>When you ask for DATEDIFF("yyyy",2000,2001) VBScript treats those numbers as *DAY NUMBERS*. (Where day 1 is December 21, 1899, by the way.) And since those are only 1 day apart, patently they are also ZERO years apart.<BR><BR>If you are going to store YEARS in your variables, then you simply do a NUMBER subtraction, not a DateDiff!!!<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
234

## RE: Because 2000 and 2001 ...

I did number subtraction too it dint work too....<BR><BR>Any clues or hints will shine my day!!

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sigh...show your code...

...for the number subtraction *and* the DEBUG output, showing the numbers again.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
234

## getting result but....

using the below code i am getting the result of 1 but when i take the response.end statement off.... it is giving me an error saying...<BR>-------------------------------------------------<BR><BR>Error Type:<BR>Microsoft VBScript runtime (0x800A000D)<BR>Type mismatch: &#039;CSng&#039;<BR><BR>-----------------------------------------------------<BR><BR>Dim FirstNum, SecondNum<BR> FirstNum=session("strEmpFromYear"&i) <BR> SecondNum=session("strEmpToYear"&i) <BR><BR>Dim SubResult<BR> <BR> SubResult = (CSng(SecondNum) - CSng(FirstNum))<BR> <BR> Response.Write "Subtraction = " &SubResult &"<BR>"<BR> <BR> &#039;Response.End

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Then the error is AFTER...

...the Response.End.<BR><BR>Some place else in your code you are using CSNG and getting that error.<BR><BR>Oh, wait!<BR><BR>THis is in a loop, yes?<BR><BR>So obviously for some value of i FirstNum or SecondNum is *not* a valid number. <BR><BR>Time to DEBUG!<BR><BR>&#060;%<BR>...<BR>For i = xx To yy &#039; or whatever you are using to loop on i<BR> FirstNum=session("strEmpFromYear"&i) <BR> SecondNum=session("strEmpToYear"&i) <BR> If Not IsNumeric(FirstNum) OR Not IsNumeric(SecondNum) Then<BR> Response.Write "BUG: When i is " & i _<BR> & " then FirstNum is " & FirstNum & ", SecondNum is " & SecondNum<BR> Response.End<BR> End If<BR> Dim SubResult<BR> SubResult = (CSng(SecondNum) - CSng(FirstNum))<BR> Response.Write "Subtraction = " &SubResult &"&#060;br/&#062;"<BR> ...<BR>Next &#039; or however you continue the loop<BR><BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
234

## RE: Then the error is AFTER...

no luck it is pointing to the bug...

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oh, *THAT* is real informative!

&#062; no luck it is pointing to the bug...<BR><BR>What is "IT"? What is "THE BUG"?<BR><BR>Have you perhaps neglected to fix your browser?<BR><BR>(1) Click on TOOLS <BR>(2) Click on INTERNET OPTIONS <BR>(3) Click on the ADVANCED tab <BR>(4) Find the checkbox labeled "Show friendly HTTP error messages" <BR>(5) *UNCHECK* it! <BR>(6) Say OKAY to get back to browser <BR>(7) Close all browsers <BR>(8) Open a new browser <BR>(9) Navigate to your problem page <BR>(10) Read the *DEBUG* message instead of the supposedly "friendly" error message one. <BR>(11) Copy/paste *that* here. <BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•