objective-cで日付や曜日を取得する際についての質問です。
lang
1 NSDate *today = [NSDate date]; 2 NSCalendar *calendar = [NSCalendar currentCalendar]; 3 NSUInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday; 4 NSDateComponents *comps = [[NSDateComponents alloc] init]; 5 6 [comps setDay:-1]; 7 [comps setWeekday:-1]; 8 9 NSDate *date = [calendar dateByAddingComponents:comps toDate:today options:0]; 10 NSDateComponents *comps2 = [calendar components:flags fromDate:date]; 11 NSLog(@"%@",date); 12 NSInteger Tomonth = comps2.month; 13 NSInteger Today = comps2.day; 14 NSInteger Toweek = comps2.weekday; 15 16 NSLog(@"今日は%ld日",(long)Today); 17 NSLog(@"今日は%ld何曜日",(long)Toweek);
このようなコードで現在の日付と曜日の1日前を取得したいのですが,
NSLogで確認してみると二日前の日にちと曜日の数字が2引かれた数値が表示されてしまいます。
この解決策を知っている方おりましたら教えてください。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/05/22 02:26