Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the number of months between two dates using Cocoa

How do I calculate the number of months between two dates using Cocoa?

Thanks, Stan

like image 658
Stan92 Avatar asked Sep 28 '09 21:09

Stan92


1 Answers

components:fromDate:toDate:options doesn't work correctly for following example:

begin date: Jan 01, 2012 end date: March 31, 2012.

num of months using above method = 2

The correct answer should be 3 months.

I am using the long way of calculations as follows: 1. Find number of days in the beginning month. Add it to the fraction part of the month. If the first date is the beginning of the month, count it as full month. 2. find the number of days in the end month. Add it to the fraction part of the monthIf the date is the last of the month count it as a full month. 3. Find the whole/full months between the 2 dates and add to the integer part of the month. 4. Add the integer & fraction parts of the months to get a correct value.

like image 189
Gamma-Point Avatar answered Oct 29 '22 05:10

Gamma-Point