6

iOSで現在の日付の日、月、年を確認したい。ネットで検索したところ、これが見つかりました。

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];
NSInteger Day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

このコードは正しい日を示していますが、年と月が正しく表示されていません。2147483647のような数字が表示されます。解決策が見つからなかったので、見つけたコードを修正するのを手伝ってください。または、必要なことを実行する新しいコードを作成してください。

4

1 に答える 1

13

この行が問題です:

NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];

そのはず

NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
于 2012-07-12T08:59:34.313 に答える