Results 1 to 9 of 9
  1. #1
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43

    the date difference in the form of yyyy mm dd

    Hello peoples!
    I have two columns, as in the picture. How to make the difference between these two dates in the third column shows that difference as yyyy mm dd.



    Thanks!
    Attached Thumbnails Attached Thumbnails Datum od do.PNG  

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    You do date math with DATEDIFF.
    format is irrelevant since all dates are numeric.

  3. #3
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43
    Quote Originally Posted by ranman256 View Post
    You do date math with DATEDIFF.
    format is irrelevant since all dates are numeric.
    I did this for months, but how to show me for example
    2 years 8 months 15 days.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    A common reason for doing this is to calculate somebody's age
    Ranman is correct about using DateDiff but its not quite as simple as that

    Using:
    Code:
    Difference: DateDiff("yyyy",[StartDate],[EndDate]) & " " & DateDiff("m",[StartDate],[EndDate]) & " " & DateDiff("d",[StartDate],[EndDate])
    will give you:

    Click image for larger version. 

Name:	Capture1.PNG 
Views:	15 
Size:	5.9 KB 
ID:	34611

    Method 2 is better but still unhelpful
    Code:
    Difference: DateDiff("yyyy",[StartDate],[EndDate]) & " " & DateDiff("m",[StartDate],[EndDate]) Mod 12 & " " & DateDiff("d",[StartDate],[EndDate]) Mod 365
    Click image for larger version. 

Name:	Capture2.PNG 
Views:	15 
Size:	5.5 KB 
ID:	34612

    Getting the number of days is quite tricky due to leap years

    Method 3:
    Code:
    Format(CDate(CLng([EndDate])-CLng([StartDate])),"yyyy mm dd")
    This is still unhelpful. Try it & you'll see why
    You can fiddle around with this and combine it with method 2
    However, I suggest you use code specifically designed for the purpose e.g. http://allenbrowne.com/func-08.html
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43
    Thanks ridders52!
    I changed a little your code, which would look like this:
    Expr1: DateDiff ("yyyy"; [StartDate]; [EndDate]) & "years" & DateDiff ("m"; [StartDate]; [EndDate]) Mod 12 & "month" & DateDiff ("d" ]; [EndDate]) Mod 365
    As you can see the difference is just in: you put "," and I have made a ";"
    I have little trouble with the days, I'll show you in the picture I got

  6. #6
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43
    I got this by implementing your code
    Attached Thumbnails Attached Thumbnails godine mjeseci dani.PNG  

  7. #7
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43
    Sorry ridders52 in a hurry I did not read your previous post well

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,127
    As I said, you could use method 2 for years & months but not days

    Method 3 gives the days but the years & months both need adapting!
    Click image for larger version. 

Name:	Capture3.PNG 
Views:	11 
Size:	6.7 KB 
ID:	34617

    Method 3 is solvable e.g. by subtracting 1900 from the years, subtract 1 from the months then adjust the days (but needs MUCH more checking!)
    There are reasons for the above due to the start date for date calculations in Access (Dec 30 1899 was day zero)

    However, a custom function will do it all for you e.g. Allen Browne's function in the link I provided
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    Starmo is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jun 2018
    Location
    Usa
    Posts
    43
    Thank you very much ridders 52!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 10-16-2017, 11:47 AM
  2. Replies: 1
    Last Post: 01-18-2014, 08:33 PM
  3. Replies: 3
    Last Post: 01-03-2014, 03:14 AM
  4. Can access format a date in an odd way? DD-MMM-YYYY
    By redbull in forum Programming
    Replies: 3
    Last Post: 08-23-2013, 11:34 AM
  5. Replies: 12
    Last Post: 03-09-2011, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums