0

我是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];
    }

}
4

5 回答 5

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];
    }
 }
于 2013-10-16T11:24:30.023 回答
0

In addition to Simon's pointer issue, your dates will include the time info and so is never likely to be identical. May be the same day but not the same time. You need to find if the day matches. I do this by:

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];
    }

}
于 2013-10-16T11:28:17.213 回答
0
if(myDate==now)

正在比较 2 个指针而不是 2 个日期。您需要使用以下代码

if([myDate compare:now] == NSOrderedSame)
于 2013-10-16T11:13:18.910 回答
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,请原谅任何拼写错误或语法错误。

于 2013-10-16T12:19:15.103 回答
0

斯威夫特 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)
   }
}
于 2017-10-27T11:23:47.597 回答