English 中文(简体)
Number of weeks in month
原标题:

I have the following code:

NSDate *dateNow = [[NSDate alloc] init];
NSTimeInterval timeDifference = [usersDate timeIntervalSinceDate:dateNow];
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];

// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeDifference sinceDate:date1]; 

// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSWeekCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

NSLog(@"Break down: %d Years, %d months, %d Weeks, %d days %d Hours, %d Minutes, %d Seconds", [breakdownInfo year], [breakdownInfo month], [breakdownInfo week], [breakdownInfo day], [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]);

It prints output such as:

Break down: 0 Years, 5 months, 3 Weeks, 4 days 2 Hours, 48 Minutes, 20 Seconds

I actually do not need the number of months until the date, but the total of the number of weeks, is there anyway I can modify the code to do this? I do not want to simply assume that 1 month = 4 weeks as this would undoubtably be wrong.

Kind Regards

最佳回答

Doesn t it work to just use:

unsigned int unitFlags = NSWeekCalendarUnit;

?

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签