开发者

NSDate question

I am calculating the months differences between two dates using :

NSInteger month = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit fromDate: start toDate: end options: 0] month];

When it calculates the months, i am having a small issue. Lets assume that :

startdate = 01/05/2005; (dd/mm/yyyy) enddate = 01/06/2005; (dd/mm/yyyy)

Month difference is returned as 0. I noticed that to return the correct result (in this instance being 1) , i need to set the enddate to +1day开发者_JS百科s of the start date. So, 02/06/2005. How could i fix this?


Try to set the timeZone to UTC.

Here's example code that returns 1 for the difference.

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


But the difference is 0 months, as it's the same month. If you prefer to represent the same month as one, and the 'next' month as 2, can you simply add 1 to the result?


Sounds to me like your input is being misinterpreted. How are you creating the start and end objects? Have you verified that your input data is being interpreted as dd/mm/yyyy as you intended, rather than mm/dd/yyyy?

If the first component ('01' for both start and end) is being interpreted as the month instead of the day, a difference of 0 would be perfectly correct. Also, and using '02' in the end date would (again, correctly) behave the way you describe if that is the case.


This worked for me, but I haven't given it too much though as to whether it's the best solution:

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

My goal here was to ignore anything above hour. That's why I set the NSDayCalendarUnit-flag. If you set the NSMonthCalendarUnit-flag (?), it should provide you with months in total, and not just dateA's month - dateB's month.

Edit: I got this from another thread, but couldn't figure out which, so thank you whoever provided me with this piece of code :)


You're not doing this, and you should be...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜