0

2 つの日付の間の時間が 3 か月を超えているかどうかを確認しようとしていますが、成功していません。これが私が試みていることです。私は何か間違ったことをしていますか?昨日detailTextDateを設定しても、評価ラベルが表示されます。dateFormat を設定する必要がありますか?

    NSString *detailTextDate = @"August 11, 2013";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterLongStyle;

    NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *todaysDate = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSMonthCalendarUnit
                                                        fromDate:startDate
                                                          toDate:todaysDate
                                                         options:0];
    if(components.month >= 3){
        evaluationLabel.hidden = NO;
    }

}

//tried using NSYearCalendarUnit, Month, Day etc.

//startDate = 2013-08-11 06:00:00 +0000, detailTextDate = August 11, 2013, components = //<NSDateComponents: 0x176c3c70>
//    Month: 0
4

6 に答える 6

0
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MMMM dd, yyyy"];
    NSDate *date1 = [dateFormatter dateFromString:detailTextDate];

    NSDate *todaysDate = [NSDate date];

    unsigned int unitFlags = NSDayCalendarUnit,NSMonthCalendarUnit,NSYearCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:todaysDate  options:0];

    int month = [comps month];
    NSLog(@"%d",month);

    if(month>0)
    {

    }
于 2013-08-21T06:27:46.193 に答える
0

問題を解決しました。理由はわかりませんが、これは機能します。将来誰かを助けることを願っています。

    NSString *detailTextDate = @"May 23, 2013";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateStyle = NSDateFormatterLongStyle;



    NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *todaysDate = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:todaysDate
                                                         options:0];

        NSLog(@"startDate = %@, detailTextDate = %@, components = %ld", startDate, detailTextDate, (long)components.day);
    if(components.day >= 90){
        evaluationLabel.hidden = NO;
    }

}
于 2013-08-21T04:12:34.903 に答える
0

12次の[component month]および 9 のログ[component day](例では、月の差がゼロになるため、開始日を変更したことに注意してください):

  NSString *detailTextDate = @"August 11, 2012";

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  dateFormatter.dateStyle = NSDateFormatterLongStyle;

  NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
  NSDate *todaysDate = [NSDate date];

  NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                   initWithCalendarIdentifier:NSGregorianCalendar];

  NSUInteger flags = NSMonthCalendarUnit | NSDayCalendarUnit;
  NSDateComponents *components = [gregorianCalendar components:flags
                                                      fromDate:startDate
                                                        toDate:todaysDate
                                                       options:0];

  NSLog(@"%ld", (long)[components month]);
  NSLog(@"%ld", (long)[components day]);
于 2013-08-21T04:04:14.193 に答える
-1
 NSString *detailTextDate = @"August 11, 2013";

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateStyle = NSDateFormatterLongStyle;

NSDate *enddate = [dateFormatter dateFromString:detailTextDate];
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
NSInteger secondsBetweenDates = distanceBetweenDates / 60;

if(secondsBetweenDates>0){
// Here you can compare seconds
}
于 2013-08-21T04:32:48.843 に答える