我已經試了很多方法,但是獲得的日期都不是當期日期。這是現在的代碼:
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd-MM-yyy"];
NSDate *currentDate = [NSDate date];
NSDate *fromDate = [NSString stringWithFormat:@"%@",[tempFormatter stringFromDate:currentDate]];
NSLog(@"currentDate %@", fromDate);
NSDate *toDate = [NSString stringWithFormat:@"%@",[tempFormatter stringFromDate:datePicker.date]];
NSLog(@"toDate %@", toDate);
NSTimeInterval interval = [toDate timeIntervalSinceDate:fromDate];
double leftDays = interval/86400;
NSLog(@"Total interval Between::%g",leftDays);
不知道錯誤在哪兒了,謝謝幫忙。
你的這行代碼有問題:
NSDate *toDate = [NSString stringWithFormat:@"%@",[tempFormatter stringFromDate:datePicker.date]];
你的代碼中只有fromDate
是NSDate
類型
所以把你的代碼改成這樣:
NSDate *currentDate = [NSDate date];
NSDate *toDate = datePicker.date;
NSTimeInterval interval = [toDate timeIntervalSinceDate:currentDate];
肯定會有用的