如何在 iPhone 中将字符串值转换为日期?
我想在我的项目中将字符串值转换为日期,因为我正在从 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;
}
I want convert string value into date in my project because I am fetching XML value from web. In XML file I get weather condition and there all value in this weather condition I get current date but my forecastcondition is not come in date it comes in string value, like this:
friday saturday and sunday
Like this value are come in my forecast condition day this string value come in Day_of_week [Day_of_week]
means it shows like this:
friday,saturday,sunday
How can I convert this string value with date?
This is my controller class.m file
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜你的问题是未来的日期是“星期四”、“星期五”……?
基本上,您需要将该文本转换为星期几数字(最简单的方法是“ifladder”,但如果您想要区域设置敏感,您可以使用更复杂的方案)。然后计算出今天的星期几数字并从第一个数字中减去该数字。如果结果为负,则加 7。(如果结果为零,则可能意味着它指的是今天,也可能意味着它指的是从今天起的一周 - 如果您想要后者,如果结果为零或负数,则加 7。)然后,要计算输入的星期几表示的日期,请将上述计算结果乘以 ONE_DAY 与当前日期相加。
更多详细信息
当然,您知道什么是“ifladd”:
要从当前日期获取星期几,请使用类似以下内容的方法:
从dayNumber 中减去todayNumber。如果结果为负,则在结果上加 7。然后,该值告诉您未来“日”有多少天,您可以从中计算出它的日期。
I gather that your problem is that future dates are coming in as "Thursday", "Friday", ...?
Basically, you need to convert that text to a day-of-week number (simplest approach is an "if ladder", though if you want to be locale-sensitive you can use a more complex scheme). Then figure out today's day-of-week number and subtract that from the first number. If the result is negative, add 7. (If the result is zero, it may mean that it refers to today, or it may mean it refers to a week from today -- if you want the latter, add 7 if the result is zero or negative.) Then, to compute the date represented by the input day of the week, add the above computed result multiplied times ONE_DAY to the current date.
More detail
You know what an "if ladder" is, of course:
To get the day of week from the current date use something like:
Subtract todayNumber from dayNumber. If the result is negative, add 7 to the result. That value then tells you how many days in the future "day" is, and from that you can calculate it's date.
使用 NSDateFormatter 在
NSString
和NSDate
Use NSDateFormatter to convert between
NSString
andNSDate
您可以使用“datefromstring”来解决您的问题。
这里我有一个例子:
希望这会对您有所帮助。
you can use "datefromstring" for ur problem.
here i've an example for this:
Hope this will help you.