1

iOSの年齢確認システムを作りたい

ユーザーが UITextField を介して入力する日付があります。この日付を現在の日付と比較して、ユーザーが 18 歳未満かどうかを確認するにはどうすればよいですか。

これが私がこれまでに行ったことですが、今日の年-生年= 18であっても、明らかに人々の年齢が18歳未満である可能性があるのは年だけです。

 NSArray *ageArray = [ageString componentsSeparatedByString:@"/"];

    int day = [[ageArray objectAtIndex:0] intValue];
    int month = [[ageArray objectAtIndex:1] intValue];
    int year = [[ageArray objectAtIndex:2] intValue];

    if(day <= 31 && month <= 12){

        NSDateFormatter* df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"MM/dd/yyyy"];
        NSDate* userDate = [df dateFromString:ageString];


        NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:userDate];
        NSDateComponents *today = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];

        NSLog(@"%d - %d\n%d", [today year], [otherDay year], ([today year] - [otherDay year]));

        if([today year] - [otherDay year] =< 18) {
            NSLog(@"too young");

            if([today month] < [otherDay month])



        }
4

2 に答える 2

0

これは次の方法で実行できます。

NSDate *dateA = [NSDate date];
NSDate *dateB = [NSDate date]; //User's age

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                               fromDate:dateA
                                                 toDate:dateB
                                                options:0];

NSLog(@"Difference in date components: %i/%i/%i", components.day, components.month, components.year);

if (components.year < 18) {
    //Under Age
} else {
    //Ok Age
}

その NSLog では、これらの日付の違いが得られます

于 2012-07-26T14:21:32.753 に答える
-2

このコードは、NSTimeInterval を日数に変換する必要があります。

NSTimeInterval time = [enteredDate timeIntervalSinceDate:currentDate];
NSInteger days  = time/60/60/24;

これを使用して、設定された日数と比較できます。

于 2012-07-26T14:13:31.557 に答える