NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorianCalendar components:(NSYearCalendarUnits | NSMonthCalendarUnits | NSWeekCalendarUnits | NSWeekdayCalendarUnits) fromDate:[NSDate date]];
comps.weekday = 2;
NSDate *monday = [gregorianCalendar dateFromComponents:comps];
comps.weekday = 3;
NSDate *tuesday = [gregorianCalendar dateFromComponents:comps];
comps.weekday = 4;
NSDate *wednesday = [gregorianCalendar dateFromComponents:comps];
comps.weekday = 5;
NSDate *thursday = [gregorianCalendar dateFromComponents:comps];
Getting wrong dates
-
27-11-2021 - |
Question
I'm trying to get the dates of monday-thursday in the current week. But for some reason this piece of code gives me a mix of this weeks dates and the previous weeks days.
I know this might be a strange way of doing this, but I really can't figure out what is wrong with the code.
If someone got a better way of getting the dates pls give a hint then :)
My code:
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];
Solution
OTHER TIPS
You can use the following to get the day of the week as a number, this is psuedocode, so may not compile, but it should give you a good start. Depending on the number, you can generate your other dates and then get them as strings back later.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"c"]; //<- gives you day of week as an integer, 1 for monday, 2 for tuesday, etc
NSDate *today = [NSDate date];
NSInteger dayOfWeek = [[df stringFromDate:today] integerValue];
then create the other NSDates by adding day offsets to your original today variable.
then format them back out.
[df setDateFormat:@"yyyy/mm/dd"];
[df stringFromDate:today]; //<-- do this for all the other dates too
[df release]; //<- if you do this method often, consider having the date formatter as global
Here is a good reference for formatting. http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns