473,792 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1915
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_Initializ e()

End Sub

Private Sub Class_Terminate ()

End Sub

Public Function GetDateSpan(dtm 1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy" ,dtmStartDate,d tmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYe ars)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy", intYears,dtmSta rtDate)
End If
intMonths = DateDiff("m",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMo nths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",int Months,dtmStart Date)
End If
intDays = DateDiff("d",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDa ys)
End Function

Private Function GetPlural(intnu m1)
If IsNumeric(intnu m1) 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.c om> wrote in message news:<uM******* *******@TK2MSFT NGP10.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_Initializ e()

End Sub

Private Sub Class_Terminate ()

End Sub

Public Function GetDateSpan(dtm 1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy" ,dtmStartDate,d tmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYe ars)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy", intYears,dtmSta rtDate)
End If
intMonths = DateDiff("m",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMo nths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",int Months,dtmStart Date)
End If
intDays = DateDiff("d",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDa ys)
End Function

Private Function GetPlural(intnu m1)
If IsNumeric(intnu m1) 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.c om> wrote in message news:<uM******* *******@TK2MSFT NGP10.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_Initializ e()

End Sub

Private Sub Class_Terminate ()

End Sub

Public Function GetDateSpan(dtm 1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy" ,dtmStartDate,d tmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYe ars)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy", intYears,dtmSta rtDate)
End If
intMonths = DateDiff("m",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMo nths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",int Months,dtmStart Date)
End If
intDays = DateDiff("d",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDa ys)
End Function

Private Function GetPlural(intnu m1)
If IsNumeric(intnu m1) 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.c om> wrote in message
news:d0******** *************** ***@posting.goo gle.com...
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.c om> wrote in message

news:<uM******* *******@TK2MSFT NGP10.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_Initializ e()

End Sub

Private Sub Class_Terminate ()

End Sub

Public Function GetDateSpan(dtm 1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy" ,dtmStartDate,d tmEndDate)
GetDateSpan = intYears & " Year" & GetPlural(intYe ars)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy", intYears,dtmSta rtDate)
End If
intMonths = DateDiff("m",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMo nths) If intMonths > 0 Then
dtmStartDate = DateAdd("m",int Months,dtmStart Date)
End If
intDays = DateDiff("d",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDa ys) End Function

Private Function GetPlural(intnu m1)
If IsNumeric(intnu m1) 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_Initializ e()

End Sub

Private Sub Class_Terminate ()

End Sub

Public Function GetDateSpan(dtm 1,dtm2)
dtmStartDate = dtm1
dtmEndDate = dtm2
intYears = DateDiff("yyyy" ,dtmStartDate,d tmEndDate)
If Month(dtmStartD ate) > Month(dtmEndDat e) And intYears > 0 Then
intYears = intYears - 1
End If
GetDateSpan = intYears & " Year" & GetPlural(intYe ars)
If intYears > 0 Then
dtmStartDate = DateAdd("yyyy", intYears,dtmSta rtDate)
End If
intMonths = DateDiff("m",dt mStartDate,dtmE ndDate)
If Day(dtmStartDat e) > Day(dtmEndDate) And intMonths > 0 Then
intMonths = intMonths - 1
End If
GetDateSpan = GetDateSpan & " " & intMonths & " Month" & GetPlural(intMo nths)
If intMonths > 0 Then
dtmStartDate = DateAdd("m",int Months,dtmStart Date)
End If
intDays = DateDiff("d",dt mStartDate,dtmE ndDate)
GetDateSpan = GetDateSpan & " " & intDays & " Day" & GetPlural(intDa ys)
End Function

Private Function GetPlural(intnu m1)
If IsNumeric(intnu m1) 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.c om> wrote in message news:<OK******* *******@TK2MSFT NGP10.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
8009
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 until completion: DateDiff("d",,) is TODAY the appropriate syntax for today's date? Could the function/expression even be set up that way, or do I need to do something else to get 'days until completion'? (My Access manuals do not mention a TODAY...
4
11919
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 STARTINGDATE1 ENDINGDATE1 STARTINGDATE2
3
1944
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 DateOfBirth. The standard format for this field is yy.mm.dd (ie- 73.02.01). And on the form that the field is brought into the input mask for the field is 00.99.99;0;_ What i want to do is have another field on the form that generates the persons...
3
3355
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 boxes act like message boards to alert the user if the days elapsed have exceeded a certain amount of
5
6692
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 Access. I have a database where they customer wants to purge records from certain tables after three years. When I was asked to make the changes I originally thought this was an incredibly simple thing to do. I looked at the function list and...
7
15506
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 DateDiff in VB.NET which is only available in C# if you don't mind linking in Microsoft.VisualBasic.dll to your C# application. I wanted to avoid this so set about a pure C# solution which uses a combination of TimeSpan in whole days and the...
3
13431
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# FileDate = #2/4/2006 4:49:02 PM# TranDay = DatePart(DateInterval.DayOfYear, TransmissionDate) 'Returns 36 FileDay = DatePart(DateInterval.DayOfYear, FileDate) 'Returns 35 DayDiff = DateDiff(DateInterval.DayOfYear, FileDate, TransmissionDate) 'Returns...
4
3970
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 = DateFormat.getDateInstance(DateFormat.MEDIUM); Date firstDate = df.parse(firstRequest); Date secondDate = df.parse(secondRequest); int requestDays = DateDiff(dd, firstDate, secondDate);
14
3579
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 Argument Date1 cannot be converted to type Date. an anyone clear this up. If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd, "hh:mm AMPM")) <= 0 Then 'logout MsgBox("A shift change has occurred. You will be logged
0
9670
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10430
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.