これは、目的のcの現在の時間の差を30分に4時間加算したいと考えています。現在の時間に30分を加算する
例:11.30から16.00
11.00から15.30 11.45私はこれを行うことができますどのように
15.30に?
これは、目的のcの現在の時間の差を30分に4時間加算したいと考えています。現在の時間に30分を加算する
例:11.30から16.00
11.00から15.30 11.45私はこれを行うことができますどのように
15.30に?
4時間30分の添加がNSCalendar
の-dateByAddingComponents:toDate:options:
で行うことができ、かつ丸めが-nextDateAfterDate:matchingUnit:value:options:
で行うことができます:ここNSCalendarMatchStrictly
は、夏時間の変更を越え、取得すること
NSDate *now = [NSDate date];
// This assumes you want to use the Gregorian calendar.
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
// Add 4 hours and 30 minutes
NSDateComponents *fourAndAHalfHours = [NSDateComponents new];
fourAndAHalfHours.hour = 4;
fourAndAHalfHours.minute = 30;
NSDate *future = [calendar dateByAddingComponents:fourAndAHalfHours toDate:now options:NSCalendarMatchNextTimePreservingSmallerUnits];
// Now we want to round to next 30 minutes. If we're past the 30 minute mark, round to 0.
// Otherwise, round to 30.
NSInteger minutes = [calendar component:NSCalendarUnitMinute fromDate:future];
NSInteger roundTo = minutes >= 30 ? 0 : 30;
NSDate *rounded = [calendar nextDateAfterDate:future matchingUnit:NSCalendarUnitMinute value:roundTo options:NSCalendarMatchStrictly];
注意変更後の次の使用可能時間を表示します。異なる行動を探している場合は、NSCalendarMatchNextTimePreservingSmallerUnits
とNSCalendarMatchNextTime
もご覧ください。
ありがとうございました...その作業 –
このNSDateを丸めた方法を教えてください:* [calendar nextDateAfterDate:future matchingUnit:NSCalendarUnitMinute value:roundTo options:NSCalendarMatchStrictly]; 24時間形式の時刻 –
@evelinprissy [NSDateFormatter'](https://developer.apple.com/reference/foundation/nsdateformatter)を使用して日付を取得し、必要な形式の文字列に変換することができます24時間を含む。 –
これを試してみてください:
NSDate *dt = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:dt];
comps.hour = 4;
if(comps.minute >= 30)
comps.minute = -(comps.minute-30);
else
comps.minute = -comps.minute;
comps.second = -comps.second;
NSDate *newDate = [cal dateByAddingComponents:comps toDate:dt options:0];
これは明らかではありません。詳細を記入できますか? – Apurv
時間が現在の時刻から4時間30分に加算されます。現在の時間11.11が表示されている場合15.30 11.30は16.00(24時間形式)を意味します –
あなたは何を求めようとしていますか? – Himanth