以月为单位的周数 [英] Number of weeks in month

查看:135
本文介绍了以月为单位的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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]);

打印输出:

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

我实际上不需要直到日期为止的月数,但是总的周数,有没有反正我可以修改代码这样做?我不想简单地假设1个月= 4周,因为这无疑是错误的。

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.

种类问题

推荐答案

它不工作只是使用:

unsigned int unitFlags = NSWeekCalendarUnit;

这篇关于以月为单位的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆