2012-04-27 8 views
0

私は現在の日付+時刻から30日後までを見つけるために次のコードを使用しています。それはうまく動作します。しかし、私は今終了日は30日ではなく、60日でなければなりません。どのように私は60日まで得るために、以下のコードを変更することができますか?終了日を[currentDateComponents month] +1に変更しようとしましたが、動作しません。助けてください?iPhone:今後60日間の現在の日付を検索

NSDate *date = [NSDate date]; 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *currentDateComponents = [gregorian components:(NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit) fromDate:date]; 
NSLog(@"- current components year = %d , month = %d , week = % d, weekday = %d", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]); 

NSArray* calendars = [[CalCalendarStore defaultCalendarStore] calendars]; 
NSLog(@"calendars.count: %ld", [calendars count]); 

debug(@"LogMsg:%@ Date:%@", @"Start looking for new events for pushing iCal to OfferSlot", [NSDate date]); 

// 30 days any new or modified calendar (iCal) events will be pushed here to OfferSlot 
NSInteger year = [[NSCalendarDate date]yearOfCommonEra]; 
NSDate *startdate = [NSCalendarDate dateWithYear:year month:[currentDateComponents month] day:1 hour:0 minute:0 second:0 timeZone:nil]; 
NSDate *enddate = [NSCalendarDate dateWithYear:year month:[currentDateComponents month] day:31 hour:23 minute:59 second:59 timeZone:nil]; 

答えて

3

あなたのコードは、テキストに記載されている内容を必ずしも意味するものではありません。現在の月の初めに1つ、月末に1つ(月が31日の場合、31日未満の場合はendDateが次の月になる)の2つの日付が作成されます。毎月1日にコードを実行すると、将来30日分のNSDateが作成されますが、毎月1日にのみ発生します。


あなたは本当に今、このコードを使用してから60日であるNSDateを取得したい場合:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *sixtyDaysOffset = [[NSDateComponents alloc] init]; 
sixtyDaysOffset.day = 60; 
NSDate *sixtyDaysFromNow = [calendar dateByAddingComponents:sixtyDaysOffset toDate:[NSDate date] options:0]; 
+0

が魅力のように働いた、ありがとうございます! – Getsy

関連する問題