如何在iPhone中将字符串值转换为日期?

时间:2011-07-21 11:38:13

标签: iphone objective-c nsstring nsdate nsdateformatter

我想在我的项目中将字符串值转换为日期,因为我从web获取XML值。在XML文件中,我得到了天气状况,并且在这种天气条件下我得到当前日期的所有值,但是我的预测条件不是以字符串值出现的日期,如下所示:

  星期六星期六和星期天

就像这个值来自我的预测条件日这个字符串值来Day_of_week [Day_of_week]意味着它显示如下:

  

周五,周六,周日

如何将此字符串值与日期一起转换?

这是我的控制器class.m文件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TWeatherCell *cell =(TWeatherCell *) [MyTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell_%d", indexPath.row]];
    if (cell == nil) {
        //cell = [[[TWeatherCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier] autorelease];
        cell = [[[TWeatherCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"cell_%d", indexPath.row]] autorelease];
    }
        //ForecastCondition *cond=[forecastcond objectAtIndex:0];
    ForecastCondition *cond1=[forecastcond objectAtIndex:1];
    ForecastCondition *cond2=[forecastcond objectAtIndex:2];
    ForecastCondition *cond3=[forecastcond objectAtIndex:3];

    NSDate *today = [NSDate date]; // some valid date, let's say today is thursdaya
    NSDate *thursday = [today dateWithTimeInterval:1 *ONE_DAY sinceDate:cond1.Dayofweek];
    NSDate *friday = [today dateWithTimeInterval:2 * ONE_DAY sinceDate:cond2.Dayofweek];
    NSDate *saturday = [today dateWithTimeInterval:3 * ONE_DAY sinceDate:cond3.Dayofweek];
    if ([currentcond.Icon isEqualToString:@"http://\n"])
    {
        cell.weatherimage.image = [UIImage imageNamed:@"listIcon-H.png"];
    }
    else {
    NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:currentcond.Icon]];       
        NSLog(@"this is image from server:%@",imageData);
        cell.weatherimage.image = [UIImage imageWithData:imageData];
        [imageData release];
    }
    NSDateFormatter *date_formatter=[[NSDateFormatter alloc]init]; 
    [date_formatter setDateFormat:@"dd-MM-yyyy"]; 
    switch (indexPath.row) {
        case 0:
    NSLog(@"%d",indexPath.row);
            NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:currentcond.Icon]];
            NSLog(@"this is image from server:%@",imageData);
            cell.weatherimage.image = [UIImage imageWithData:imageData];
            [imageData release];

    cell.reportdate.text = _forecastInfo.CurrentDateTime;
    cell.conditionname.text = currentcond.Condition;
    cell.twotemp.text = [NSString stringWithFormat:@"Temp:%@/%@",currentcond.Tempf,currentcond.Tempf];
    cell.twodirection.text = currentcond.WindCondition;
    cell.humidity.text = currentcond.Humidity;

    break;
        case 1:
            NSLog(@"%d",indexPath.row);

            cell.reportdate.text = cond1.Dayofweek;
            cell.conditionname.text = cond1.Condition;
            cell.twotemp.text = [NSString stringWithFormat:@"Temp:%@/%@",cond1.Low,cond1.High];
            //NSDateFormatter *date_formatter=[[NSDateFormatter alloc]init]; 
//          [date_formatter setDateFormat:@"dd-MM-yyyy"]; 

            //[cond.Dayofweek dateWithTimeIntervalSinceNow:(60*60*24)*1];
            //NSDate *date1 = [date_formatter dateFromString:cond.Dayofweek]; 
            //NSDate *= NSDate *date1 ;
            //NSDate *date1 = [NSDate date];
            //NSString *strDate = [date_formatter stringFromDate:date1]; 


            break;
        case 2:

            NSLog(@"%d",indexPath.row);
            cell.reportdate.text = cond2.Dayofweek;
            cell.conditionname.text = cond2.Condition;
            cell.twotemp.text = [NSString stringWithFormat:@"Temp:%@/%@",cond2.Low,cond2.High];
            break;
        case 3:
            NSLog(@"%d",indexPath.row);
            cell.reportdate.text = cond3.Dayofweek;
            cell.conditionname.text = cond3.Condition;
            cell.twotemp.text = [NSString stringWithFormat:@"Temp:%@/%@",cond3.Low,cond3.High];
            break;
        default:
            NSLog(@"Out of Range ",indexPath.row);
            break;
    }
    return cell;
}

4 个答案:

答案 0 :(得分:0)

使用NSDateFormatterNSStringNSDate

之间进行转换

答案 1 :(得分:0)

你可以使用“datefromstring”来解决你的问题。 我在这里有一个例子:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy "];
NSDate *date1 = [dateFormatter dateFromString:YourDate_String];
 NSLog(@"Date: %@",date1);
[dateFormatter release];

希望这会对你有所帮助。

答案 2 :(得分:0)

我认为你的问题是未来的日期会以“星期四”,“星期五”开始,......?

基本上,您需要将该文本转换为星期几的数字(最简单的方法是“if ladder”,但如果您想要对语言环境敏感,则可以使用更复杂的方案)。然后计算出今天的星期几数字,并从第一个数字中减去该数字。如果结果是否定,则添加7.(如果结果为零,则可能意味着它指的是今天,或者它可能意味着它指的是从今天开始的一周 - 如果您想要后者,如果结果为零或负,则添加7。)然后,要计算由周输入日表示的日期,请将上述计算结果乘以ONE_DAY乘以当前日期。

更多细节

你知道什么是“if ladder”,当然:

if ([day isEqualToString:@"Sunday"]) {
    dayNumber = 1;
}
else if ([day isEqualToString:@"Monday"]) {
    dayNumber = 2;
}
else if ([day isEqualToString:@"Tuesday"]) {
....

要从当前日期获取星期几,请使用以下内容:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
NSInteger todayNumber = [components weekday];

从dayNumber减去todayNumber。如果结果为负,请将7添加到结果中。然后,该值会告诉您未来“天”的天数,并从中可以计算出它的日期。

答案 3 :(得分:-1)

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
[df setDateFormat:@"YYYY-MM-dd"]; 
NSDate* date = [df dateFromString:@"date string"];

NSLog(@"Converted date is %@",date);