View Full Version : Date Calculation
petertran123
10-08-2002, 02:50 PM
Hi Whammy and others,
Thank you for your valueable time of helping me. I have a question about Date Calculation..
there are 2 problems of date calculation, but i can't figure it out to make it run
first: i have a yearly field: to have user field out their year of birth then i subtract by current year to get their age:
second: i have paydate field in format 10/11/2002
use the PayStubDate and the CurrentDate to calculate the number of days past since the beginning of the year (DaysPast)
can you help me with the script and make it calculates.
Thanks
whammy
10-08-2002, 07:37 PM
Number of the day in the year:
Dim Jan1, Today, ThisYear, NoOfDays
ThisYear = Year(Now)
Jan1 = "1/1/" & ThisYear
Today = Date()
NoOfDays = DateDiff("d",Jan1,Today)+1
Response.Write("Today is day number " & NoOfDays & " of 2002.")
You can also use the DateDiff function to figure out the person's age, I'll get back to you on that in a little bit.
whammy
10-08-2002, 08:56 PM
Age function:
Dim Birthday, Birthyear, Age
Birthday = "12/31/"
Birthyear = "1970"
Offset = DateDiff("d",Date(),Birthday & Year(Date()))
Age = DateDiff("yyyy",Date(),Birthday & Birthyear)*-1
If OffSet >0 Then Age = Age - 1
Response.Write("Your Age is: " & Age)
:)
petertran123
10-09-2002, 04:37 PM
Whammy,
thanks for your help
when i use this code to implace line Birthyear = "1970" to
BirthYear = Len(request.form("enteryourbirthyear") is not working do you know what is cause an error?
petertran123
10-09-2002, 04:45 PM
nevermind Whammy i fixed it:) thanks
whammy
10-09-2002, 05:17 PM
For month, day, and year, you might even try a select dropdown box... i.e.
<%
Dim indent, i
indent = " "
Response.Write("<form id=""monthform"" action=""months.asp"" method=""post"">" & vbCrLf)
Response.Write("<select name=""month"">" & vbCrLf)
For i = 1 to 12
If CStr(i) = Request.Form("month") Then
Response.Write(indent & "<option value=""" & i & """ selected=""selected"">" & i & "</option>" & vbCrLf)
Else
Response.Write(indent & "<option value=""" & i & """>" & i & "</option>" & vbCrLf)
End If
Next
Response.Write("</select>" & vbCrLf)
Response.Write("<input type=""submit"" value=""Submit"">" & vbCrLf)
Response.Write("</form>" & vbCrLf)
%>
petertran123
10-09-2002, 06:51 PM
i used code above and change a little bit to work with my form
Dim Birthday, Birthyear, Age, Offset
Birthday = "10/09/"
Birthyear = Cstr(Request.Form("ByearOldestDep"))
Offset = DateDiff("d", date(),Birthday & Year(Date()))
Age = DateDiff("yyyy", Date(),Birthday & Birthyear)*-1
If Offset > 0 Then Age = Age - 1
how do you padding from the empty field. If Birthyear field is empty i want to padding and return 0 age
petertran123
10-09-2002, 08:32 PM
i'm using this code
Dim Jan1, Today, ThisYear, DaysPast, DailyGross, EstYEGross
ThisYear = Year(Now)
Jan1 = "1/1/" & ThisYear
Today = Cstr(Request.Form("PayStubDate"))
DaysPast = DateDiff("d",Jan1,Today)+1
DailyGross = Today / DaysPast ---------> this is where i got error********
EstYEGross = DailyGross * 365
but everytime i excute code and received error
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "10/01/2002"]'
whammy
10-10-2002, 01:10 AM
Originally posted by petertran123
i'm using this code
Dim Jan1, Today, ThisYear, DaysPast, DailyGross, EstYEGross
ThisYear = Year(Now)
Jan1 = "1/1/" & ThisYear
Today = Cstr(Request.Form("PayStubDate"))
DaysPast = DateDiff("d",Jan1,Today)+1
DailyGross = Today / DaysPast ---------> this is where i got error********
EstYEGross = DailyGross * 365
but everytime i excute code and received error
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "10/01/2002"]'
That's because you're saying:
Today = Cstr(Request.Form("PayStubDate"))
Which makes "Today" a string. Then you try to divide an integer by a string. Doesn't work.
Besides, I'm not sure what you're trying to calculate, but that isn't right... can you explain in words what you're calculating exactly?
If you just want to get the number of days since Jan 1st, then you don't need to divide by anything, since that's what the DateDif() function is already figuring out for you. (You can't divide by a date, anyway)...
Oh yeah, and regarding your first post... put an if/then statement around the code after you make sure that they entered something for the fields... (just like you'd validate anything else)... that's one of the reasons I suggested a dropdown...
petertran123
10-10-2002, 02:05 PM
Dear Whammy,
thank you for your kind reponse. i feel guilty to cause lots of problem to you as i'm a beginner of ASP
as the second question i figured it out and it ran fine finally, but the first question i still have a little problem. i modified the code and have it runs okay, but the field can't padding when it blank..please take a look at code below.
Dim Birthday, Birthyear, Age, Offset
Birthday = "10/09/"
Birthyear = Cstr(Request.Form("ByearOldestDep"))
Offset = DateDiff("d", date(),Birthday & Year(Date()))
Age = DateDiff("yyyy", Date(),Birthday & Birthyear)*-1
If Offset > 0 Then Age = Age - 1
If Age > "17" then
AppDec = "D"
DeclineCode = "14"
SecondDecApp()
else
AppDec = "A"
DeclineCode = "00"
Holidayloan4()
end if
this code is runs fine if someone enter their birthyear, but if they are not enter and leave the birthyear field blank? this is where i have a problem! i wanted something to be able to pass the blank field and calculate less 17. If they are less than 17 then they will go to Holidayloan4 if the birthyear is entered great than 17 then go to SecondDecApp as you see code above. I think i can use if/then state to pass the value when the field is blank, but i'm not sure where i put it. Please help..
:confused:
whammy
10-11-2002, 01:24 AM
If Age > "17" OR Len(Age) = 0 Then
AppDec = "D"
DeclineCode = "14"
SecondDecApp()
Else
AppDec = "A"
DeclineCode = "00"
Holidayloan4()
End If
I think that's what you're looking for, but I'm not sure since it's hard for me to understand you (no offense, I'm sure your English is much better than my understanding of your native language (Vietnamese? I'm just curious as to where you're from. :D )).
But that should give you the general idea... ?
Also, please note how I indented the code... that not only helps YOU when you look at it again after 6 months and writing 200 other applications, but if another developer has to work on it, it's really helpful to them.
I always recommend indenting code (even in HTML), to improve the readability of your code. :)
petertran123
10-11-2002, 02:07 PM
Whammy you are the man, you know where i am come from and what i needed :) how's greatful you are. Once again, thank you very much for your time and supported.
God bless you,
petertran123
10-18-2002, 04:09 PM
Whammy,
i have a little problem here. Can you look at a code and tell me what is wrong Please
*****************Primary info**************
Dim Jan1, Today, ThisYear, DaysPast, DailyGross, EstYEGrossPr, PayStubGross
ThisYear = Year(Now)
Jan1 = "1/1/" & ThisYear
Today = Cstr(Request.Form("PayStubDate"))
DaysPast = DateDiff("d",Jan1,Today)+1
PayStubGross = Cstr(Request.Form("PayStubGross"))
DailyGross = Ccur(PayStubGross) / DaysPast
EstYEGrossPr = DailyGross * 365
****************Spouse Info******************
Dim JanSp, TodaySp, ThisYearSp, DaysPastSp, DailyGrossSp, EstYEGrossSp, PayStubGrossSp
ThisYearSp = Year(Now)
JanSp = "1/1/" & ThisYearSp
TodaySp = Cstr(Request.Form("PayStubGrossSp"))
DaysPastSp = DateDiff("d",JanSp,TodaySp)+1
PayStubGrossSp = Cstr(Request.Form("PaystubGrossSp"))
DailyGrossSp = Ccur(PayStubGrossSp) / DaysPast
EstYEGrossSp = DailyGrossSp * 365
**************add primary info and spouse info**********
Dim EstYEGross
EstYEGross = EstYEGrossPr + EstYEGrossSp
** code are working fine, until i try to test the form and leave SPOUSE INFO Blank and i start received an error at Spouse Info line **DaysPastSp = DateDiff("d",JanSp,TodaySp)+1** Is there anyway i can padding this? If the spouse field are not enter and it blanks i want the form still continue and calculate for PRIMARY INFO.
Thanks,
petertran123
10-18-2002, 07:22 PM
Whammy,
i added if..then statement and i got it's working :thumbsup:
Thanks,
whammy
10-19-2002, 01:21 AM
Cool! :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.