我正在寻找一些代码,可以让我的uipicker像这样填充 我们在这里讨论两个组成部分。第一个组件需要DAY和DATE 我可以处理的第二个组件。
|星期三20日|下午2点|
|星期四21日下午1点
|等...
我可以处理第二个组件,我只是找不到任何示例代码来格式化第一个组件。我知道我需要首先确定今天的DAY,然后DATE ..然后从那里构建,但希望有人可能有一些类似于此的示例代码。 任何帮助将不胜感激。
答案 0 :(得分:3)
因为没有找到任何其他方式或更好的方法来做到这一点。我把它放在一起。它正在创建一个格式化的日期数组,就像我想要的那样。它不漂亮,但我找不到更好的方法。你会认为FOR循环可以更容易地设置格式化日期的最后2个字符,但事实证明它有它的怪癖,我不想花一个小时试图纠正它,所以我采取了简单的丑陋方式。< / p>
下面的代码生成了Day&amp;的格式化数组。这样的日期:
星期三5日
星期四6日
星期五7日
等等...
从今天的日期和日期开始。
- (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];
}
}
答案 1 :(得分:1)
以下将为您提供日期名称和日期:
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];