1

How can i calculate duration between two dates. likeenter image description here enter image description here I am getting contacts birthday list from contacts framework. Now i want to compare Today date with user DOB value and i should display the "remaining days count"(birthday).

For Example: In contacts i added one user like: DOB: dd/MM/yyyy -->12/04/1960

now today date is: 25/04/2016

I want to get the upcoming birthday date(duration between two dates)

o/p: 18 days

How can i get this

Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
  • 2
    possible duplicate of http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates?rq=1 and http://stackoverflow.com/questions/4575689/objective-c-calculating-the-number-of-days-between-two-dates – Niall Cosgrove Apr 25 '16 at 11:36
  • Hello, @user6170930, why you're giving negative mark to my question. whatever you suggested me those links are calculating hole days like (user DOB:28/04/1958) and today date is: 25/04/2016==> it's calculating the whole difference, but i am looking for only Result like:3 days – Mannam Brahmam Apr 25 '16 at 12:50
  • Please tell me the solution for this? -- @user6170930 – Mannam Brahmam Apr 25 '16 at 12:50

2 Answers2

3

Try below line of code. you will get the solution.

NSDateComponents *components;
NSInteger days;

components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit 
    fromDate: startDate toDate: endDate options: 0];
days = [components day];
Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51
0

Use the following

unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |     NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [currCalendar components:unitFlags fromDate:fistDate   toDate:secondDate  options:0];
int months = [conversionInfo month]; // to get number of months
int days = [conversionInfo day]; //  to get number of days
int hours = [conversionInfo hour]; // to get hour difference
int minutes = [conversionInfo minute]; // to get hour difference

copied from Difference between two NSDates

Community
  • 1
  • 1