Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 日期问题_Iphone_Objective C_Xcode_Sdk - Fatal编程技术网

Iphone 日期问题

Iphone 日期问题,iphone,objective-c,xcode,sdk,Iphone,Objective C,Xcode,Sdk,我使用以下公式计算两个日期之间的月差: NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: start toDate: end options: 0] month]; 当它计算月份时,我遇到了一个小问题。让我们假设: 起始日期=2005年5月1日;(年月日) 截止日期=2005年6月1日;(年月日) 月差返回为0。我注意到要返回正确的结果(在本例中为1),我需要将e

我使用以下公式计算两个日期之间的月差:

NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: start toDate: end options: 0] month];
当它计算月份时,我遇到了一个小问题。让我们假设:

起始日期=2005年5月1日;(年月日) 截止日期=2005年6月1日;(年月日)


月差返回为0。我注意到要返回正确的结果(在本例中为1),我需要将enddate设置为开始日期的+1天。所以,2005年6月2日。我怎样才能解决这个问题

但是差别是0个月,因为是同一个月。如果您更喜欢将同一个月表示为1,将“下一个”月表示为2,您可以简单地将1添加到结果中吗?

听起来您的输入被误解了。如何创建开始和结束对象?您是否已验证您的输入数据是否按预期解释为dd/mm/yyyy,而不是mm/dd/yyyy


如果第一个组件(“01”表示开始和结束)被解释为月份而不是日期,那么0的差值将是完全正确的。此外,如果是这样的话,在结束日期中使用“02”将(同样正确地)表现为您描述的方式。

这对我来说很有效,但我没有给出太多关于它是否是最佳解决方案的信息:

NSDate* dateA; // alloc and init
NSDate* dateB; // alloc and init

NSCalendar *sysCalendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSSecondCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit;

NSDateComponents *difference= [sysCalendar components:unitFlags fromDate: dateA toDate:dateB  options:0];

double hour = [difference hour];
double minute = [difference minute];
double second = [difference second];
我在这里的目标是忽略任何超过一小时的事情。这就是我设置NSDayCalendarUnit标志的原因。如果您设置了NSMonthCalendarUnit标志(?),它应该为您提供总共的月份,而不仅仅是dateA的月份-dateB的月份


编辑:我是从另一个线程获得的,但无法确定是哪一个线程,因此感谢为我提供这段代码的人:)

尝试将时区设置为UTC

下面的示例代码为差异返回1

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSDate * start = [formatter dateFromString:@"01/05/2005"];
NSDate * end = [formatter dateFromString:@"01/06/2005"];    

NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: start toDate: end options: 0] month];
NSLog(@"%@, %@ --> difference: %d", start, end, month);

你没有这样做,你应该

NSDate *start = [NSDate date];
NSDate *end = [NSDate date];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];  
start = [dateFormatter dateFromString:@"01-05-2005"];
end = [dateFormatter dateFromString:@"01-06-2005"];

NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: start toDate: end options: 0] month];
NSLog(@"%i", month);

5和6不是同一个月。:-)