現在の週の月曜日から木曜日の日付を取得しようとしています。しかし、何らかの理由で、このコードでは、今週の日付と前の週の日付が混在しています。
これは奇妙な方法かもしれませんが、コードの何が問題なのか本当にわかりません。
誰かが日付を取得するより良い方法を持っている場合は、ヒントを教えてください:)
私のコード:
NSDate *today = [NSDate date];
//Change first weekday of the week
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2;
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian1.firstWeekday = 3;
NSCalendar *gregorian2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian2.firstWeekday = 4;
NSCalendar *gregorian3 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian3.firstWeekday = 5;
NSDateComponents *components = [gregorian components:NSWeekCalendarUnit fromDate:today];
NSUInteger weekOfYear = [components weekOfYear];
//Give the date of the first week day
NSDate *mondaysdate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysdate interval:NULL forDate:today];
NSDate *tuesdaysdate = nil;
[gregorian1 rangeOfUnit:NSWeekCalendarUnit startDate:&tuesdaysdate interval:NULL forDate:today];
NSDate *wednesdaysdate = nil;
[gregorian2 rangeOfUnit:NSWeekCalendarUnit startDate:&wednesdaysdate interval:NULL forDate:today];
NSDate *thursdaysdate = nil;
[gregorian3 rangeOfUnit:NSWeekCalendarUnit startDate:&thursdaysdate interval:NULL forDate:today];
//Making the dates into string
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
formatter2.dateFormat = @"yyyy/MM/dd";
NSString *monday = [formatter2 stringFromDate:mondaysdate];
NSString *tuesday = [formatter2 stringFromDate:tuesdaysdate];
NSString *wednesday = [formatter2 stringFromDate:wednesdaysdate];
NSString *thursday = [formatter2 stringFromDate:thursdaysdate];