0

アラームを鳴らすまでの時間を確保したい場合。次に、このメソッドを使用して、翌日に day =day+1; を設定できます。あなたによると。現在時刻とアラーム設定時刻の差を返します。

4

1 に答える 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 に答える