我需要以分钟为单位计算两天之间的差异。
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSString* dateStringFromDatabase = @"01/01/1900 11:00:00 AM";
NSDate* dateFromString = [outputFormatter dateFromString:dateStringFromDatabase];
NSString* a = [outputFormatter stringFromDate:now];
NSDate* b = [outputFormatter dateFromString:a];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:dateFromString
toDate:b options:0];
int hours = [components hour];
int minutes = [components minute];
self.lblResult.text = [NSString stringWithFormat:@"%i:%i", hours, minutes];
但我有以下问题: 对于当前时间,我得到:
2012-10-21 07:37:09 +0000 // wrong it's two hours late
当我尝试投射@"01/01/1900 11:00:00 AM"
到NSDate
我得到
1900-01-01 10:00:00 +0000 // also two hours late