473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Datediff good question

Hi, I got the next question, I want to know the diference between two
dates
and thats is really easy, but my problem is get that diference in the
following format

ex. "the diference is= 0 year, 9 months, 11 days"

var1 = 04/04/1999
var2 = 01/11/2000
Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days
<br>")
Response.Write("var1 to var2 is " & DateDiff("m", var1, var2) & "
months <br>")
Response.Write("var1 to var2 is " & DateDiff("yyyy", var1, var2) & "
year(s) <br>")

Results:
var1 = 04/04/1999
var2 = 01/11/2000
var1 to var2 is 282 days
var1 to var2 is 9 months
var1 to var2 is 1 year(s)

I want to be like this: "the diference is= 0 year, 9 months, 11 days"

I will be very pleased if somebody can helpme to make a Fuction to do
this.
Jul 19 '05 #1
7 1899
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/1/01","6/2/04")
Set DT = Nothing

Class DateTool
Private dtmStartDate
Private dtmEndDate

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub

Public Function GetDateSpan(dtm1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy",dtmStartDate,dtmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYears)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy",intYears,dtmStartDate)
End If
intMonths = DateDiff("m",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMonths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",intMonths,dtmStartDate)
End If
intDays = DateDiff("d",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDays)
End Function

Private Function GetPlural(intnum1)
If IsNumeric(intnum1) Then
If intnum1 > 1 Then
GetPlural = "s"
End If
End If
End Function
End Class

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.
Jul 19 '05 #2
Thank you very much, it works like a charm you save my ass.

thanks

"dlbjr" <oo**@iforgot.com> wrote in message news:<uM**************@TK2MSFTNGP10.phx.gbl>...
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/1/01","6/2/04")
Set DT = Nothing

Class DateTool
Private dtmStartDate
Private dtmEndDate

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub

Public Function GetDateSpan(dtm1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy",dtmStartDate,dtmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYears)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy",intYears,dtmStartDate)
End If
intMonths = DateDiff("m",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMonths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",intMonths,dtmStartDate)
End If
intDays = DateDiff("d",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDays)
End Function

Private Function GetPlural(intnum1)
If IsNumeric(intnum1) Then
If intnum1 > 1 Then
GetPlural = "s"
End If
End If
End Function
End Class

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.

Jul 19 '05 #3
Hello again, I though it was working good, but I found one problem:
everythings works all raight but when you use something like this

DT.GetDateSpan("4/6/2004","6/4/2004")

I get a negative day, in that case -2

Ex.
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/6/2004","6/4/2004") & "<BR>"
Set DT = Nothing

output:
0 Year 2 Months -2 Day

Do you have any idea why?

Thanks :)

"dlbjr" <oo**@iforgot.com> wrote in message news:<uM**************@TK2MSFTNGP10.phx.gbl>...
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/1/01","6/2/04")
Set DT = Nothing

Class DateTool
Private dtmStartDate
Private dtmEndDate

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub

Public Function GetDateSpan(dtm1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy",dtmStartDate,dtmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYears)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy",intYears,dtmStartDate)
End If
intMonths = DateDiff("m",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMonths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",intMonths,dtmStartDate)
End If
intDays = DateDiff("d",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDays)
End Function

Private Function GetPlural(intnum1)
If IsNumeric(intnum1) Then
If intnum1 > 1 Then
GetPlural = "s"
End If
End If
End Function
End Class

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.

Jul 19 '05 #4
Seems right to me...

It appears to be calculating them individually as opposed to collectively.
You're probably going to need to do some math on this one.
"Drago" <kp****@yahoo.com> wrote in message
news:d0**************************@posting.google.c om...
Hello again, I though it was working good, but I found one problem:
everythings works all raight but when you use something like this

DT.GetDateSpan("4/6/2004","6/4/2004")

I get a negative day, in that case -2

Ex.
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/6/2004","6/4/2004") & "<BR>"
Set DT = Nothing

output:
0 Year 2 Months -2 Day

Do you have any idea why?

Thanks :)

"dlbjr" <oo**@iforgot.com> wrote in message

news:<uM**************@TK2MSFTNGP10.phx.gbl>...
Set DT = New DateTool
Response.Write DT.GetDateSpan("4/1/01","6/2/04")
Set DT = Nothing

Class DateTool
Private dtmStartDate
Private dtmEndDate

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub

Public Function GetDateSpan(dtm1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy",dtmStartDate,dtmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYears)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy",intYears,dtmStartDate)
End If
intMonths = DateDiff("m",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMonths) If intMonths > 0 Then
dtmStartDate = DateAdd("m",intMonths,dtmStartDate)
End If
intDays = DateDiff("d",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDays) End Function

Private Function GetPlural(intnum1)
If IsNumeric(intnum1) Then
If intnum1 > 1 Then
GetPlural = "s"
End If
End If
End Function
End Class

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted

sagacity.
Jul 19 '05 #5
'Fixed

Class DateTool
Private dtmStartDate
Private dtmEndDate

Private Sub Class_Initialize()

End Sub

Private Sub Class_Terminate()

End Sub

Public Function GetDateSpan(dtm1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy",dtmStartDate,dtmEndDate)
If Month(dtmStartDate) > Month(dtmEndDate) And intYears > 0 Then
intYears = intYears - 1
End If
GetDateSpan = intYears & " Year" & GetPlural(intYears)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy",intYears,dtmStartDate)
End If
intMonths = DateDiff("m",dtmStartDate,dtmEndDate)
If Day(dtmStartDate) > Day(dtmEndDate) And intMonths > 0 Then
intMonths = intMonths - 1
End If
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMonths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",intMonths,dtmStartDate)
End If
intDays = DateDiff("d",dtmStartDate,dtmEndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDays)
End Function

Private Function GetPlural(intnum1)
If IsNumeric(intnum1) Then
If intnum1 > 1 Then
GetPlural = "s"
End If
End If
End Function
End Class

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.
Jul 19 '05 #6
Change line 38 to:

If intnum1 <> 1 Then

This will make the text read properly.

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.
Jul 19 '05 #7
Thank you again, it's working great.

:)

"dlbjr" <oo**@iforgot.com> wrote in message news:<OK**************@TK2MSFTNGP10.phx.gbl>...
Change line 38 to:

If intnum1 <> 1 Then

This will make the text read properly.

'from dlbjr

'Unambit from meager knowledge of inane others,engender uncharted sagacity.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
3
by: chanchito_cojones | last post by:
i have a question regarding the DateDiff function. I am quite new to access and seem to have hit a snag with this function. My problem is as follows: I have a table field that list a persons...
3
by: T23Ij9 | last post by:
Hi. I have 3 seperate date fields. InitialDate InspDate ReportDate I am trying to setup several unbound Text boxes in a form that will give me days elapsed between these dates. These text...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
3
by: Price Brattin | last post by:
Why is the DateDiff function in the following code returning zero? Dim FileDate, TransmissionDate as Date Dim TranDay, FileDay, DayDiff as Inteter TransmissionDate = #2/5/2006 1:57:56 PM#...
4
by: Brita | last post by:
You guys have been life savers for me, and I appreciate your help so much. I am trying to check the difference in days between two submitted requests. DateFormat df =...
14
by: cmdolcet69 | last post by:
I'm trying to use the DateDiff function to calculate the difference whether a shift has been setup. when i run the code below with strFirstShiftEnd as a stringor date or datetime. I get an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.