2014-08-29 87 views

回答

-1

試試這個對我有用。

NSDateFormatter *f = [[NSDateFormatter alloc] init]; 
[f setDateFormat:@"yyyy-MM-ddHH:mm:ss"]; // your date format 
NSDate *startDate = [NSDate date]; // Today's date 
NSString * end = @"2014-08-2312:00:00"; // your date 
NSDate *endDate = [f dateFromString:end]; 

NSUInteger unitFlags = NSDayCalendarUnit; 
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [gregorianCalendar components:unitFlags 
               fromDate:startDate 
                toDate:endDate 
               options:0]; 

NSLog(@"%ld Days Left",(long)components.day); 
0

我已經發布了一個開源的類/庫來做到這一點。

看一看RelativeDateDescriptor,它可以被用來獲取的時差如下...

RelativeDateDescriptor *descriptor = [[RelativeDateDescriptor alloc] initWithPriorDateDescriptionFormat:@"%@ ago" postDateDescriptionFormat:@"in %@"]; 

// date1: 1st January 2000, 00:00:00 
// date2: 6th January 2000, 00:00:00 
[descriptor describeDate:date2 relativeTo:date1]; // Returns '5 days ago' 
[descriptor describeDate:date1 relativeTo:date2]; // Returns 'in 5 days' 
相關問題