Josh Kahane Josh Kahane - 1 month ago 8
iOS Question

Use of NSDate to get dates

I have attempted this is many ways but failed consistently, hopefully you can help me achieve the what I want to do.

I am making an iPad app, I will have five tables in a single view and each table will have a date as a header/title in this format, e.g. Monday 20

These five tables will be Monday to Friday. This is the bit I can't do. I want to work out the current date and then highlight the table which is today, obviously changes everyday.

So for example, lets say today is Thursday 9th. Thursday table is highlighted and then is automatically sets the date of the other tables around Thursday.

Think of a school timetable/planner/diary. Monday to Friday, each labelled with their dates.

EDIT: So what if I did it like this? If I add this into the code you gave me, if TRUE (button pressed) add seven days, done just like in the example form Apple. However my issue, what is gregorian? what do I replace it with? I have seen it used lots on calendar samples from Apple.

if (tableView == monTable){
if(next == TRUE){
[comps setDay:7];
NSDate *date = [gregorian dateByAddingComponents:comps toDate:curDate options:0];
}
else{
[comps setWeekday:2];
}
}

Answer

You can get Monday to Friday dates this way:

NSDate* curDate = [NSDate date]; // Get current date
NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
for (int i = 2; i <= 6; i++){ 
    [comps setWeekday:i];
    NSDate *tDate = [calendar dateFromComponents:comps];
    NSLog(@"%@", tDate);
}

To determine which date to highlight (current date) you just need to check date's weekday component.

Edit: titleForHeaderInSection method may look like:

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section{
    NSDate* curDate = [NSDate date]; // Get current date
    NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
    if (tableView == monTable)
        [comps setWeekday:2];
    if (tableView == tueTable)
        [comps setWeekday:3];
    if (tableView == wedTable)
        [comps setWeekday:4];
    if (tableView == thuTable)
        [comps setWeekday:5];
    if (tableView == friTable)
        [comps setWeekday:6];

    NSDate *tDate = [calendar dateFromComponents:comps];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"EEE, MMM d"];


    return [formatter stringFromDate:tDate];
}
Comments