Seperate DateDIff Function

  • Thread starter Thread starter aldunford
  • Start date Start date
A

aldunford

I used the DateDiff Function
to return an answer as 1year 2months 5days

How can I seperate so each has their own column/field?

1 year 2 months 5 days(original column)

1 years (column 1)

2 months (column 2)

5 days (column3)
 
Since DateDiff doesn't return answers like that, I'm assuming you're using a
user-defined function to figure out the differences.

One approach would be to rewrite the function so that you pass it a
parameter saying which value you want it to pass.
 
I used the function that you wrote to get the answer.

http://www.accessmvp.com/djsteele/Diff2Dates.html


I just need to manipulate it so I can get it to give me years, months, days
in seperate column/fields

I have been able to get it to give me the following:

7 years 4 months 6 days this would be the orginal answer

I can get it to give me just the 7 years. But when I change it to Months it
returns 88 and then days is 620. I need it to be 4 months and then 6 days.

This is the last step to my whole database and i'm just going crazy trying
to figure it out. I can do it manually no problem but converting it to
Access....I was able to do my calculations in Excel but then it was in
seperate columns that I manually entered in to.

Thanks for your help and Thanks for the original function!! It works great!!
 
I can get it to give me just the 7 years. But when I change it to Months it
returns 88 and then days is 620. I need it to be 4 months and then 6 days.

If you really want to get a date decomposed into three fields so that you can
put it back together again into one field - which seems really complex and
roundabout - try

Yearcount: DateDiff("yyyy", [datefield], Date())
Monthcount: DateDiff("m", [datefield], Date()) - 12*DateDiff("yyyy",
[datefield], Date())
Daycount: IIf(Day([datefield]) <= Day(Date()), Day(Date()) - Day([datefield]),
Day(Date()) + Day(DateSerial(Year(Date()), Month(Date()) + 1, 0) -
Day([datefield])

Untested air code but it should give you a start.
 
Thanks I have gotten it to give me the following:

The Years works great so I have no problems with that one!

This is what I used for the Month

Monthcount: DateDiff("m",[Prior State Service Hire Date],[Prior State
Service Resign Date],Date())-12*DateDiff("yyyy",[Prior State Service Hire
Date],[Prior State Service Resign Date],Date())

This works however it gives me -4 (Which is year - 4 months) This is fine as
I was able to write this

ActualMonths: IIf([Monthcount]<=0,[Monthcount]+12,[Monthcount]) This gives
me the actual months PERFECT!!!

I just can't get the days to work

Daycount: IIf(Day( [Prior State Service Hire Date] , [Prior State Service
Resign Date] ) <= Day(Date()), Day(Date()) - Day( [Prior State Service Hire
Date] , [Prior State Service Resign Date] ),
Day(Date()) + Day(DateSerial(Year(Date()), Month(Date()) + 1, 0) -
Day( [Prior State Service Hire Date] , [Prior State Service Resign Date] )

It tells me wrong number of arguments. Any suggestions how to pull the date?



John W. Vinson said:
I can get it to give me just the 7 years. But when I change it to Months it
returns 88 and then days is 620. I need it to be 4 months and then 6 days.

If you really want to get a date decomposed into three fields so that you can
put it back together again into one field - which seems really complex and
roundabout - try

Yearcount: DateDiff("yyyy", [datefield], Date())
Monthcount: DateDiff("m", [datefield], Date()) - 12*DateDiff("yyyy",
[datefield], Date())
Daycount: IIf(Day([datefield]) <= Day(Date()), Day(Date()) - Day([datefield]),
Day(Date()) + Day(DateSerial(Year(Date()), Month(Date()) + 1, 0) -
Day([datefield])

Untested air code but it should give you a start.
 
Aldumford,

Below is a test function and the original function modified to return
the items you requested. If you are trying to use this in a query,
however, it would take some extra work to change the output and would
probably require 3 calls to get the 3 different results.

Ron

====================================

Function testdiff()
Dim my As Integer
Dim mm As Integer
Dim md As Integer

Diff2Dates "ymd", #1/1/2006#, Date, my, mm, md
MsgBox my
MsgBox mm
MsgBox md
End Function


'***************** Code Start **************
Public Function Diff2Dates(Interval As String, Date1 As Date, Date2 As
Date, OY As Integer, OM As Integer, OD As Integer, _
Optional ShowZero As Boolean = False) As Variant
'Author: ) Copyright 2001 Pacific Database Pty Limited
' Graham R Seach MCP MVP (e-mail address removed)
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
' This code is freeware. Enjoy...
' (*) Amendments suggested by Douglas J. Steele MVP
'
'Description: This function calculates the number of years,
' months, days, hours, minutes and seconds between
' two dates, as elapsed time.
'
'Inputs: Interval: Intervals to be displayed (a string)
' Date1: The lower date (see below)
' Date2: The higher date (see below)
' ShowZero: Boolean to select showing zero elements
'
'Outputs: On error: Null
' On no error: Variant containing the number of years,
' months, days, hours, minutes & seconds between
' the two dates, depending on the display interval
' selected.
' If Date1 is greater than Date2, the result will
' be a negative value.
' The function compensates for the lack of any intervals
' not listed. For example, if Interval lists "m", but
' not "y", the function adds the value of the year
' component to the month component.
' If ShowZero is True, and an output element is zero, it
' is displayed. However, if ShowZero is False or
' omitted, no zero-value elements are displayed.
' For example, with ShowZero = False, Interval = "ym",
' elements = 0 & 1 respectively, the output string
' will be "1 month" - not "0 years 1 month".

On Error GoTo Err_Diff2Dates

Dim booCalcYears As Boolean
Dim booCalcMonths As Boolean
Dim booCalcDays As Boolean
Dim booCalcHours As Boolean
Dim booCalcMinutes As Boolean
Dim booCalcSeconds As Boolean
Dim booSwapped As Boolean
Dim dtTemp As Date
Dim intCounter As Integer
Dim lngDiffYears As Long
Dim lngDiffMonths As Long
Dim lngDiffDays As Long
Dim lngDiffHours As Long
Dim lngDiffMinutes As Long
Dim lngDiffSeconds As Long
Dim varTemp As Variant

Const INTERVALS As String = "dmyhns"

'Check that Interval contains only valid characters
Interval = LCase$(Interval)
For intCounter = 1 To Len(Interval)
If InStr(1, INTERVALS, Mid$(Interval, intCounter, 1)) = 0 Then
Exit Function
End If
Next intCounter

'Check that valid dates have been entered
If Not (IsDate(Date1)) Then Exit Function
If Not (IsDate(Date2)) Then Exit Function

'If necessary, swap the dates, to ensure that
'Date1 is lower than Date2
If Date1 > Date2 Then
dtTemp = Date1
Date1 = Date2
Date2 = dtTemp
booSwapped = True
End If

Diff2Dates = Null
varTemp = Null

'What intervals are supplied
booCalcYears = (InStr(1, Interval, "y") > 0)
booCalcMonths = (InStr(1, Interval, "m") > 0)
booCalcDays = (InStr(1, Interval, "d") > 0)
booCalcHours = (InStr(1, Interval, "h") > 0)
booCalcMinutes = (InStr(1, Interval, "n") > 0)
booCalcSeconds = (InStr(1, Interval, "s") > 0)

'Get the cumulative differences
If booCalcYears Then
lngDiffYears = Abs(DateDiff("yyyy", Date1, Date2)) - _
IIf(Format$(Date1, "mmddhhnnss") <= Format$(Date2,
"mmddhhnnss"), 0, 1)
Date1 = DateAdd("yyyy", lngDiffYears, Date1)
End If

If booCalcMonths Then
lngDiffMonths = Abs(DateDiff("m", Date1, Date2)) - _
IIf(Format$(Date1, "ddhhnnss") <= Format$(Date2,
"ddhhnnss"), 0, 1)
Date1 = DateAdd("m", lngDiffMonths, Date1)
End If

If booCalcDays Then
lngDiffDays = Abs(DateDiff("d", Date1, Date2)) - _
IIf(Format$(Date1, "hhnnss") <= Format$(Date2,
"hhnnss"), 0, 1)
Date1 = DateAdd("d", lngDiffDays, Date1)
End If

If booCalcHours Then
lngDiffHours = Abs(DateDiff("h", Date1, Date2)) - _
IIf(Format$(Date1, "nnss") <= Format$(Date2, "nnss"), 0,
1)
Date1 = DateAdd("h", lngDiffHours, Date1)
End If

If booCalcMinutes Then
lngDiffMinutes = Abs(DateDiff("n", Date1, Date2)) - _
IIf(Format$(Date1, "ss") <= Format$(Date2, "ss"), 0, 1)
Date1 = DateAdd("n", lngDiffMinutes, Date1)
End If

If booCalcSeconds Then
lngDiffSeconds = Abs(DateDiff("s", Date1, Date2))
Date1 = DateAdd("s", lngDiffSeconds, Date1)
End If

If booCalcYears And (lngDiffYears > 0 Or ShowZero) Then
varTemp = lngDiffYears & IIf(lngDiffYears <> 1, " years", "
year")
OY = lngDiffYears
End If

If booCalcMonths And (lngDiffMonths > 0 Or ShowZero) Then
If booCalcMonths Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMonths & IIf(lngDiffMonths <> 1, " months",
" month")
OM = lngDiffMonths
End If
End If

If booCalcDays And (lngDiffDays > 0 Or ShowZero) Then
If booCalcDays Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffDays & IIf(lngDiffDays <> 1, " days", "
day")
OD = lngDiffDays
End If
End If

If booCalcHours And (lngDiffHours > 0 Or ShowZero) Then
If booCalcHours Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffHours & IIf(lngDiffHours <> 1, " hours", "
hour")
End If
End If

If booCalcMinutes And (lngDiffMinutes > 0 Or ShowZero) Then
If booCalcMinutes Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffMinutes & IIf(lngDiffMinutes <> 1, "
minutes", " minute")
End If
End If

If booCalcSeconds And (lngDiffSeconds > 0 Or ShowZero) Then
If booCalcSeconds Then
varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
lngDiffSeconds & IIf(lngDiffSeconds <> 1, "
seconds", " second")
End If
End If

If booSwapped Then
varTemp = "-" & varTemp
End If

Diff2Dates = Trim$(varTemp)

End_Diff2Dates:
Exit Function

Err_Diff2Dates:
Resume End_Diff2Dates

End Function
'************** Code End *****************
 
I can't get that to work at all. Thanks for the suggestion.

I did try to do the following

If I use the date2diff and have it give me total days

I then can do the following:

I use the Yearcount: DateDiff("yyyy",[Prior State Service Hire Date],[Prior
State Service Resign Date],Date()) to return the year of 7

Total Days ie - 2685
Totaldays/365(*years) this will give me
ie 2685/365(*7) this returns 2920

I then subtract 2920-2685 to get -235

Then Month * 30 so I get - 240

Then subtract 240-235 to get 5 days

This doesn't take into leap years so it's still off....
 
Maybe I don't get it, but would using String functions not parse out what you
need into your columns from the original answer you have already?
 
I eneded up going the long way with 2 queriesI used the datepart so that I
could get the total Years, Months, Days. Then I was able to add each of
those to the original hire date to get my answer...Here is the sql for 1st
query

SELECT [Prior State Service].[Prior State Service ID], [Prior State
Service].[SS ID#], [Prior State Service].[Prior State Service Hire Date],
[Prior State Service].[Prior State Service Resign Date], [Prior State
Service].[Prior State Service Agency], Diff2Dates("ymd",[Prior State Service
Hire Date],[Prior State Service Resign Date],True) AS [Total Years of Prior
Service], DateDiff("d",[Prior State Service Hire Date],[Prior State Service
Resign Date]) AS Days, [tblEmp Hire Info].[Isac Original Hire Date],
DateDiff("yyyy",[Prior State Service Hire Date],[Prior State Service Resign
Date],Date()) AS Yearcount, DateDiff("m",[Prior State Service Hire
Date],[Prior State Service Resign Date],Date())-12*DateDiff("yyyy",[Prior
State Service Hire Date],[Prior State Service Resign Date],Date()) AS
Monthcount, IIf([Monthcount]<=0,[Monthcount]+12,[Monthcount]) AS ActualMonth,
DatePart("yyyy",[Prior State Service Hire Date]) AS HireYear,
DatePart("yyyy",[Prior State Service Resign Date]) AS ResignYear,
[ResignYear]-[HireYear] AS TotalYears, DatePart("m",[Prior State Service Hire
Date]) AS HireMonth, DatePart("m",[Prior State Service Resign Date]) AS
ResignMonth, [ResignMonth]-[HireMonth] AS TotalMonths, DatePart("d",[Prior
State Service Hire Date]) AS HireDay, DatePart("d",[Prior State Service
Resign Date]) AS ResignDay, [ResignDay]-[HireDay] AS TotalDays,
IIf([TotalDays]<0,([TotalDays]+30),[TotalDays]) AS TrueDays,
IIf([TotalDays]<0,[TotalMonths]-1,[TotalMonths]) AS ActualMonths,
IIf([ActualMonths]<0,[ActualMonths]+12,[ActualMonths]) AS TrueMonths,
IIf([ActualMonths]<0,[TotalYears]-1,[TotalYears]) AS TrueYears
FROM [tblEmp Hire Info] INNER JOIN [Prior State Service] ON [tblEmp Hire
Info].[SS# ID] = [Prior State Service].[SS ID#];


2nd query sql

SELECT qrytotalpriorservicesumtotals.[SS ID#],
qrytotalpriorservicesumtotals.[Sum Of TrueDays],
qrytotalpriorservicesumtotals.[Sum Of TrueMonths],
qrytotalpriorservicesumtotals.[Sum Of TrueYears],
qrytotalpriorservicesumtotals.[Isac Original Hire Date], DateAdd("d",-[Sum Of
TrueDays],[Isac Original Hire Date]) AS Expr1, DateAdd("m",-[Sum Of
TrueMonths],[Expr1]) AS Expr2, DateAdd("yyyy",-[Sum Of TrueYears],[Expr2]) AS
[Prior Service Adjusted Hire Date]
FROM qrytotalpriorservicesumtotals;

I'm sure there is a easier approach to this but I have no clue! Thanks to
all for help!!
 
Back
Top