可能重复:
如何比较两个日期,返回天数.
我有两个日期(作为"yyyy-mm-dd"形式的NSString),例如:
NSString *start = "2010-11-01";
NSString *end = "2010-12-01";
Run Code Online (Sandbox Code Playgroud)
我想实施:
- (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate {
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我怎样才能比较两个日期的返回天数.例如:缺少X天的杯赛.看看我的代码.
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"d MMMM,yyyy"];
NSDate *date1 = [df dateFromString:@"11-05-2010"];
NSDate *date2 = [df dateFromString:@"11-06-2010"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
//int days = (int)interval / 30;
//int months = (interval - (months/30)) / 30;
NSString *timeDiff = [NSString stringWithFormat:@"%dMissing%d days of the Cup",date1,date2, fabs(interval)];
label.text = timeDiff; // output (Missing X days of the Cup)
Run Code Online (Sandbox Code Playgroud) 我想找到两个日期之间的总天数.
例如今天是01-01-2011(DD-MM-YYYY),第二个日期是(25-03-2011),我如何找到总天数?
NSDate *currentdate=[NSDate date];
NSLog(@"curretdate is ==%@",currentdate);
NSDateFormatter *tempFormatter1 = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter1 setDateFormat:@"dd-mm-YYYY hh:mm:ss"];
NSDate *toDate = [tempFormatter1 dateFromString:@"20-04-2011 09:00:00"];
NSLog(@"toDate ==%@",toDate);
Run Code Online (Sandbox Code Playgroud)