如果您想留出时间敲响警报。然后你可以使用这个方法,第二天你可以设置 day =day+1; 根据你。它将返回当前时间和设置的闹钟时间之间的差异。
问问题
92 次
1 回答
0
+(NSString*)getDifference:(NSString*)setTime
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:[NSDate date]];
int day=[components day];
int month=[components month];
int year=[components year];
//day=day+1;
NSDateFormatter *formatter;
NSString *CurrentdateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
CurrentdateString = [formatter stringFromDate:[NSDate date]];
//settime=@"2:40 PM";
NSDate *currentDate=[formatter dateFromString:CurrentdateString];
NSDate *setDate=[formatter dateFromString:[NSString stringWithFormat:@"%i-%i-%i %@",day,month,year,setTime]];
NSLog(@"current date is %@",[formatter stringFromDate:currentDate]);
NSLog(@"setted date string is %@",[formatter stringFromDate:setDate]);
NSTimeInterval interval=[setDate timeIntervalSinceDate:currentDate];
int totalmin=(int)(interval/60);
int hour=(int)(totalmin/60);
int min=(int)fmod(totalmin,60);
[formatter release];
if (hour<0 || min<0) {
totalmin=hour*60+min;
totalmin=(24*60)+totalmin;
hour=totalmin/60;
min=fmod(totalmin, 60);
}
NSString *returnString=[[NSString alloc] init];
returnString=[NSString stringWithFormat:@"%i hr. %i min.",hour,min];
NSLog(@"date string is %i:%i",hour,min);
return returnString;
}
于 2012-07-26T09:35:29.517 回答