How to get difference between two dates in years, months and days?

Compare two dates and get the difference in years, months, days

  • How can I know the number of years, months, weeks, days, hours and minutes from a date in the past (fixed) to now? For example I want to compare: date1: 2009/11/10 8:23 date2: 2010/12/17 10:27 and obtain as a result: 1 year, 1 month, 1 week, 2 hours, 4 minutes Many thanks!

  • Answer:

    -components:fromDate:toDate:options: method in NSCalendar class does exactly what you need - you can specify what date components you need in your difference setting corresponding flags in the 1st parameter (check reference docs for more details)

Stefano Mendicino at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

NSDate *today = [NSDate date]; NSTimeInterval interval = [today timeIntervalSinceDate:lastDate]; int weeksForLastDate=interval/604800; this gives you weeks similerly you can find years etc. so you need to implement this logic according to you and also date format according to you. here interval gives you time in seconds so convert it according to you.

Ishu

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.