我是ios开发的新手。我正在使用uipickerview制作自定义日期选择器。我有datesArray用作uipickerview的数据源。
我想知道如何仅显示标签:今天,明天,周五,周六,周日,周一,本周的周二和休息日期格式为“EEE,LLL d”。
我尝试了这段代码但是没有用。
for(int i=0;i<22;i++)
{
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * i];
NSDate *now=[NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"EEE, LLL d"];
if(myDate==now)
{
NSString *myDateString=@"today";
[datesArray addObject:myDateString];
}
else
{
NSString *myDateString = [dateFormatter stringFromDate:myDate];
[datesArray addObject:myDateString];
}
}
答案 0 :(得分:1)
试试这个。希望它有帮助
NSString *dateFromWS = @"2013-10-16";//im taking it as static you have to take string coming from webservice
for (int i = 0; i < 22; i++)
{
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * i];
NSDate *now = [dateFormatter1 dateFromString:dateFromWS];
NSDate *tomorrow = [NSDate dateWithTimeInterval:60 * 60 * 24 * 1 sinceDate:now];
//NSDate *dummy = [NSDate dateWithTimeInterval:60 * 60 * 24 * 1 sinceDate:now];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"EEE, LLL d"];
NSString *loopDate = [dateFormatter stringFromDate:myDate];
NSString *today = [dateFormatter stringFromDate:now];
NSString *tomorrowString = [dateFormatter stringFromDate:tomorrow];
if ([loopDate isEqualToString:today]) {
[datesArray addObject:@"today"];
} else if ([loopDate isEqualToString:tomorrowString]) {
[datesArray addObject:@"tomorrow"];
} else if ((i/7) < 1) {
[datesArray addObject:[loopDate substringToIndex:3]];
} else {
[datesArray addObject:loopDate];
}
}
答案 1 :(得分:0)
if(myDate==now)
比较2个指针而不是2个日期。您需要使用以下代码
if([myDate compare:now] == NSOrderedSame)
答案 2 :(得分:0)
除了Simon的指针问题,您的日期将包含时间信息,因此永远不可能相同。可能是同一天但不是同一时间。您需要查找当天是否匹配。我是这样做的:
for(int i=0;i<22;i++)
{
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * i];
NSDate *myDateNoTime = nil;
// Note: Create sysCal as a property so it is not being repeatedly recreated here
[self.sysCal rangeOfUnit:NSDayCalendarUnit startDate:&myDateNoTime interval:nil forDate:myDate];
NSDate *now=[NSDate date];
NSDate *todayDateNoTime = nil;
[self.sysCal rangeOfUnit:NSDayCalendarUnit startDate:&todayDateNoTime interval:nil forDate:now];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"EEE, LLL d"];
if([myDateNoTime isEqualToDate:todayDateNoTime])
{
NSString *myDateString=@"today";
[datesArray addObject:myDateString];
}
else
{
NSString *myDateString = [dateFormatter stringFromDate:myDate];
[datesArray addObject:myDateString];
}
}
答案 3 :(得分:0)
考虑到你的服务器日期要求,以及我认为处理选择器的更好方法,我会把它放在数据源方法中。
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *resultString = nil;
NSDate *myDate = [self dateForRow:row];
NSDate *myDateNoTime = nil;
// Note: Create sysCal as a property so it is not being repeatedly recreated here
[self.sysCal rangeOfUnit:NSDayCalendarUnit startDate:&myDateNoTime interval:nil forDate:myDate];
NSDate *now=[NSDate date];
NSDate *todayDateNoTime = nil;
[self.sysCal rangeOfUnit:NSDayCalendarUnit startDate:&todayDateNoTime interval:nil forDate:now];
if([myDateNoTime isEqualToDate:todayDateNoTime])
{
resultString = @"Today";
} else {
// Note: using property for date formatter so I am not repeatedly creating here
resultString = [self.dayFormatter stringFromDate:myDate];
}
return resultString;
}
确定行使用的日期:
-(NSDate *)dateFromRow:(NSInteger)inRow {
NSDateComponents *daysToAdd = [[NSDateComponents alloc] init];
[daysToAdd setCalendar:self.sysCal];
NSInteger daysCalc = (-1 * (inRow -2));
[daysToAdd setDay:daysCalc];
NSDate *resultDate = nil;
NSDate *calcDate = [self.sysCal dateByAddingComponents:daysToAdd
toDate:self.myServerDate
options:0];
[self.sysCal rangeOfUnit:NSDayCalendarUnit startDate:&resultDate interval:nil forDate:calcDate];
return resultDate;
}
我认为你已经按照自己的方式设置,但我认为你会发现这更加灵活,在服务器日期之前或之后的21天内没有限制。 Aircode,原谅任何拼写错误或语法错误。
答案 4 :(得分:0)
Swift 4.0
for i in 0..<22 {
var myDate = Date(timeIntervalSinceNow: 60 * 60 * 24 * i)
var now = Date()
var dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.dateFormat = "EEE, LLL d"
if myDate == now {
var myDateString = "today"
datesArray.append(myDateString)
} else {
var myDateString: String = dateFormatter.string(from: myDate)
datesArray.append(myDateString)
}
}