Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

length of service 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi - started this, thinking it was really simple but now am stuck
have a users start date dd/mm/yyyy and want to calculate the years and days to todays date.
have tried the following which looked like it was working
Code:
'works out difference in days
userlength = datediff("d",userstartdate, date)
'days left over after taking out years 
userlengthdays = (userlength MOD 365)
'works out number of years
userlengthyears = cint(datediff("YYYY", userstartdate, date))


 
Are you getting a specific error message or something other than it doesn't appear to be working?

As a thought, if you're using VBScript, then your "date" should probably read as "Now()" which will reflect the current date.

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
What if you do it this way:
Code:
'Get number of years since start
NumYears = DateDiff("yyyy", userstartdate, Now)

'Get most recent aniversary date
Aniversary = DateAdd("yyyy", NumYears, userstartdate)

'Get number of days since most recent aniversary
NumDays = DateDiff("d", Aniversary, Now)


Response.Write "Time served: "
If NumYears > 0 Then
  Response.Write NumYears & " years and "
End If
Response.Write NumDays & "days."
 
Ah dang nevermind that code I posted.
I forgot that DateDiff says there is 1 year between December 31st and January 1st.
 
Here is a revised version that doesn't rely on DateDiff to get the number of years:
Code:
'Get number of years since start
If Year(userStartDate) < Year(Now) Then
    If Month(userStartDate) < Month(Now) Then
        NumYears = Year(Now) - Year(userStartDate)
    ElseIf Month(userStartDate) > Month(Now)  
        NumYears = Year(Now) - Year(userStartDate) - 1
    Else
        If Day(Now) >= Day(userStartDate) Then
            NumYears = Year(Now) - Year(userStartDate) 
        Else
            NumYears = Year(Now) - Year(userStartDate) - 1
        End If
    End If
Else
    NumYears = 0
End If


'Get most recent aniversary date
Aniversary = DateAdd("yyyy", NumYears, userstartdate)

'Get number of days since most recent aniversary
NumDays = DateDiff("d", Aniversary, Now)


Response.Write "Time served: "
If NumYears > 0 Then
  Response.Write NumYears & " years and "
End If
Response.Write NumDays & "days."
 
thanks alot scheco - didnt know it was going to be quite so
tricky

ps - theres a missing 'then' after elseif line - if anyone else needs this
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top