Results 1 to 10 of 10

Thread: DateDiff

  1. #1

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    DateDiff

    I am trying to get total date difference between two dates in crystal formula
    for example if i enter

    Date1 17/03/2006
    Date 2 06/01/2005

    The need the result should be like 1 year 2 months ...days etc.,

    I used this formula for getting the difference in years
    But now i want the full difference in months and days

    DateDiff ('yyyy',{Date1},{Date2})

    Any ideas please
    Thanks

  2. #2
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: DateDiff

    Quote Originally Posted by wizkid
    I am trying to get total date difference between two dates in crystal formula
    for example if i enter

    Date1 17/03/2006
    Date 2 06/01/2005

    The need the result should be like 1 year 2 months ...days etc.,

    I used this formula for getting the difference in years
    But now i want the full difference in months and days

    DateDiff ('yyyy',{Date1},{Date2})

    Any ideas please
    Thanks
    i am not sure on this but i believe days would be:
    VB Code:
    1. DateDiff (d,{Date1},{Date2})
    and months would be
    VB Code:
    1. DateDiff (m,{Date1},{Date2})

  3. #3
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: DateDiff


    Hi
    I do not know that what reporting version you are using but
    Crystal Reports 8 added some new Date manipulation formulas. The DateDiff function has two forms:
    DateDiff (intervalType, startDateTime, endDateTime)
    DateDiff (intervalType, startDateTime, endDateTime, firstDayOfWeek)
    The first can be used to find the number of intervals between two dates. The IntervalType can be "yyyy" (Years), "q" quarters, "m" (months), "d" (days). See the Crystal Reports help for other variations and some useful examples.

    The second form can be used to count a specific day between two dates. It uses the "ww" IntervalType to find the number of the first day of the week.

    DateDiff ("ww", {table.datefield1}, {table.datefield2}, crWednesday)
    will tell you how many Wednesdays there are between two dates
    for more detail click on the crystal report website
    http://support.businessobjects.com/

  4. #4

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: DateDiff

    Thank you very much for your reply.
    I am using CR9.
    I can get the days and months with different formula
    usiing 'yyyy' and 'm' in interval type.
    But what i am asking is i want the entire difference in one formula

    For example if i give two dates

    Date1: 17/03/2006
    I need the result should be like 1 year 2 months ...days etc., in one formula

    for example if you give

    datediff('yyyy',date1,date2) --->the result will be only in years

    datediff('m',date1,date2)---> the result will be only in months

    etc...


    The need the result should be like 1 year 2 months ...days etc., in one formula


    Any idea?
    Last edited by wizkid; Dec 1st, 2006 at 10:21 AM.

  5. #5

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: DateDiff

    Any idea please?
    Thanks

  6. #6
    Addicted Member mabbas110's Avatar
    Join Date
    Oct 2005
    Location
    Karachi , Pakistan
    Posts
    172

    Re: DateDiff

    Here we go,

    cstr(DateDiff ("yyyy", #10/7/1999#, #2/10/2005#))+' year '+cstr(DateDiff ("m", #10/7/1999#, #2/10/2005#))+' month '
    Thanks and Regards,

    Muhammad Abbas

  7. #7

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: DateDiff

    My problem is i want the difference between two dates
    in days months and year

    For example if the two dates are 1/12/2006,3/02/2006

    Then the output should be

    0 years 2 months 2 days

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: DateDiff

    Here's a function I wrote in VB6 ages ago. By using Crystal's Basic syntax in the formula editor you may be able to duplicate its functionality. Good luck, I'm not assuring that it's bug-free.
    VB Code:
    1. Public Function IntervalBTDates(ByVal StartDate As Date, ByVal EndDate As Date) As String
    2.     '
    3.     '   Returns the interval between 2 dates in years, months & days
    4.     '
    5.     Dim dd As Long
    6.     Dim mm As Long
    7.     Dim yy As Long
    8.    
    9.     Dim ret As String
    10.     Dim tmpDate As Date
    11.     '
    12.     '   Do some basic validation before going any further
    13.     '
    14.     If StartDate > EndDate Then
    15.         MsgBox "Start date must be less than the end date.", vbExclamation, "Date Error"
    16.         Exit Function
    17.     ElseIf StartDate = EndDate Then
    18.         IntervalBTDates = "0 years, 0 months, 0 days"
    19.         Exit Function
    20.     End If
    21.    
    22.     dd = DateDiff("d", StartDate, EndDate)
    23.     mm = DateDiff("m", StartDate, EndDate)
    24.     If mm > 0 Then
    25.         If Day(StartDate) > Day(EndDate) Then
    26.             mm = mm - 1
    27.         End If
    28.     End If
    29.    
    30.     yy = mm \ 12
    31.     mm = mm Mod 12
    32.    
    33.     tmpDate = DateAdd("yyyy", -yy, EndDate)
    34.     tmpDate = DateAdd("m", -mm, tmpDate)
    35.     dd = Abs(DateDiff("d", StartDate, tmpDate))
    36.    
    37.     If yy = 1 Then
    38.         ret = "1 year, "
    39.     Else
    40.         ret = CStr(yy) & " years, "
    41.     End If
    42.    
    43.     If mm = 1 Then
    44.         ret = ret & "1 month, "
    45.     Else
    46.         ret = ret & CStr(mm) & " months, "
    47.     End If
    48.  
    49.     If dd = 1 Then
    50.         ret = ret & "1 day"
    51.     Else
    52.         ret = ret & CStr(dd) & " days"
    53.     End If
    54.  
    55.     IntervalBTDates = ret
    56.    
    57. End Function
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9

    Thread Starter
    Hyperactive Member wizkid's Avatar
    Join Date
    Dec 2005
    Location
    uk
    Posts
    405

    Re: DateDiff

    Thanks pnish
    I will try this one in crystal

  10. #10
    New Member
    Join Date
    Sep 2008
    Posts
    3

    Re: DateDiff

    Hi wizkid
    i have same problem. what u did for it in crystal report.did u try pnish method in crystal report

    Roshani

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width