简体   繁体   中英

How To Populate UIPicker with Current Day of week + Date,

I am looking for some code that will allow my uipicker to be populated like this We are talking two components here. 1st component needs DAY and DATE 2nd component I can handle.

|Wednesday 20th | 2pm |
|Thursday 21st | 1pm |
|etc...

I can handle the 2nd component, I just cant find any sample code to format the first component. I understand I would need to first determine todays DAY, then DATE.. then build from there but was hoping someone may have some sample code that is similar to this. Any help would be greatly appreciated.

For lack of finding any other way or a better way of doing this. I put this together. It is creating an array of formatted dates just like I wanted. Its not pretty but I couldnt find a better way of doing it. You would think a FOR loop would have been easier to set the last 2 characters on the formatted date, but that proved to have its quirks and I dont want to spend an hour trying to correct it so I took the easy ugly way out.

The code below produces a formated array of Day & Date such as this:
Wednesday 5th
Thursday 6th
Friday 7th
etc...
Starting with the todays day and date.

- (void)createDatesForPicker {


    daysForPicker = [[NSMutableArray alloc] initWithCapacity:0];
    formatedDaysForPicker = [[NSMutableArray alloc] initWithCapacity:0];

    //get todays Day & Date
    NSDate *today1 = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEEE dd"];

    //change how many days out you want to go here I am going 14 days out
    for (int i=0; i<14; i++){

        NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  

        NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
        [components1 setDay:([components1 day]+i)];

        NSDate *beginningOfWeek1 = [gregorian11 dateFromComponents:components1];
        NSDateFormatter *dateFormat_weekend = [[NSDateFormatter alloc] init];
        [dateFormat_weekend setDateFormat:@"EEEE dd"];
        NSString *dateString_first = [dateFormat_weekend stringFromDate:beginningOfWeek1];

        // unformatted dates array
        [daysForPicker addObject:dateString_first];

    }

    // now we have all the days and date, so lets format them so they look good 
    for (int i=0; i<[daysForPicker count];i++) {
        NSString * currentDay = [NSString stringWithFormat:@"%@",[daysForPicker objectAtIndex:i]];

        //we have date now look at 2nd to last digit and check if it is a zero, if so remove it.
        NSUInteger count = [currentDay length]-1;
        NSRange r = NSMakeRange( count,1 );
        NSString * lastChar = [currentDay substringWithRange:r];



        if ([lastChar isEqual:@"0"] == TRUE ) {
            //replace it with nothing
        }
        else {
            //not a zero at the end so we are good to remove all zeros
            currentDay = [currentDay stringByReplacingOccurrencesOfString:@"0" withString:@""]; 
        }

        //used later for date formatting
        NSUInteger count2 = [currentDay length]-2;
        NSRange r2 = NSMakeRange( count2,2 );
        NSString * dateDigits = [currentDay substringWithRange:r2];

        if ([dateDigits isEqual:@" 1"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"st"];
        }
        if ([dateDigits isEqual:@" 2"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"nd"];
        }
        if ([dateDigits isEqual:@" 3"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"rd"];
        }
        if ([dateDigits isEqual:@" 4"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@" 5"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@" 6"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@" 7"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@" 8"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@" 9"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"10"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"11"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"12"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"13"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"14"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"15"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"16"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"17"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"18"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"19"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"20"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"21"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"st"];
        }
        if ([dateDigits isEqual:@"22"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"nd"];
        }
        if ([dateDigits isEqual:@"23"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"rd"];
        }
        if ([dateDigits isEqual:@"24"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"25"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"26"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"27"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"28"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"29"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"30"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"th"];
        }
        if ([dateDigits isEqual:@"31"] == TRUE) {
            currentDay = [currentDay stringByAppendingString:@"st"];
        }


        // add formated string to array
        [formatedDaysForPicker addObject:currentDay];

    }



}

Following will give you the day name and day date:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE d"]; // name of the day and day number
dateString = [self.dateFormatter stringFromDate: aDate]; // Monday 20
[dateFormatter release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM