これが2 つの日付NSTimeInterval
の違いであることを理解する必要があります。あなたがやろうとしていることは、プログラムに次のように指示するようなものです:
日付を 2 日に設定します。
やりたいことがプログラムに次のように指示する場合:
最後の月曜日に 2 日間を加えた日付を設定します。
NSDate
したがって、次の一連のメソッドのいずれかを使用する必要があります。
+ dateWithTimeIntervalSinceNow:
+ dateWithTimeInterval:sinceDate:
+ dateWithTimeIntervalSinceReferenceDate:
+ dateWithTimeIntervalSince1970:
– initWithTimeIntervalSinceNow:
– initWithTimeInterval:sinceDate:
– initWithTimeIntervalSinceReferenceDate:
– initWithTimeIntervalSince1970:
NSDate
基準日をプラス/マイナスして作成する場所NSTimeInterval
; NSDate
オブジェクトは不変NSTimeInterval
であるため、値を使用してオブジェクトによって表される日付を変更するメソッドがないことに注意してください。
詳細については、NSDate
クラス リファレンスを参照してください。
コードを修正するには、次のようにします。
NSDate *startTime=[NSDate date] ;
NSDate *endTime=[NSDate date] ;
NSTimeInterval difference=[endTime timeIntervalSinceDate:startTime];
NSDate *newTime = [NSDate dateWithTimeInterval:difference sinceDate:startDate];