Iphone 正在尝试格式化日期
在xcode中,我使用touchXML并调用Web服务。在我的xml文件中有这样一行Iphone 正在尝试格式化日期,iphone,objective-c,xcode4,Iphone,Objective C,Xcode4,在xcode中,我使用touchXML并调用Web服务。在我的xml文件中有这样一行 <a:DateIn>2011-10-28T08:12:58.36+02:00</a:DateIn> 我想将日期转换为月:日:小时:分钟见我的帖子,我已经更新了答案。试试看是否有效。我显示实际时间,但我想显示DateIn的时间?这是一个简单的日期格式化程序问题,而不是NSDateFormatter无法处理时区偏移量中“:”的“捕获”,因此需要先对输入字符串进行按摩以删除该字符串。有人能帮
<a:DateIn>2011-10-28T08:12:58.36+02:00</a:DateIn>
我想将日期转换为月:日:小时:分钟见我的帖子,我已经更新了答案。试试看是否有效。我显示实际时间,但我想显示DateIn的时间?这是一个简单的日期格式化程序问题,而不是NSDateFormatter无法处理时区偏移量中“:”的“捕获”,因此需要先对输入字符串进行按摩以删除该字符串。有人能帮上忙吗,我需要解决方法还是什么?我应该把a:DateIn放在哪里?dateform=[NSString stringWithFormat:@“%@,[object objectForKey:@“a:DateIn”];dateform=[self-DateFormatter:dateform];我无法实现这一点,您可以编写更具体的代码,还是可以重新编写整个代码,包括dateform?在我构建之前,我放置了一个断点,以查看生成器是否运行-(NSString*)DateFormatter:(NSDate*)oldDate,但它没有,所以代码没有通过运行它?我不明白我应该把它放在代码中的什么地方,我看不出它应该如何工作…我已经更新了我的答案,这样你就可以很容易地理解我的答案。非常感谢你,但是我把时间搞错了,我应该得到08:15,但我得到00:15?是的,NSDate总是给GMT时间。例如,只需尝试
NSLog(@“%@,[NSDate])代码>。因此,这里您将获得(给定的时间+您的设备区域-以字符串格式给定的时区)作为输出。那么如何获得实际时间?
NSString *dateform;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
dateform = [NSString stringWithFormat:@"%@",[object objectForKey:@"a:DateIn"]];
NSLog(@"before dateform %@",dateform);
dateform = [self dateFormater:dateform];
NSLog(@"afters dateform %@",dateform);
}
-(NSString *)dateFormater:(NSDate *)oldDate
{
//NSDate *date1 = [[invoice objectAtIndex:0] valueForKey:@"date"];
NSString *dateStr =[NSString stringWithFormat:@"%@",oldDate];
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"YYYY-MM-dd HH:mm:ss +0000"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:@"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
[dtF release];
[dateFormatStr release];
return strDate;
}
NSString *dateform;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
dateform = [NSString stringWithFormat:@"%@",[object objectForKey:@"a:DateIn"]];
NSLog(@"before dateform %@",dateform);
dateform = [self dateFormater:dateform];
NSLog(@"afters dateform %@",dateform);
}
-(NSString *)dateFormater:(NSDate *)oldDate
{
//NSDate *date1 = [[invoice objectAtIndex:0] valueForKey:@"date"];
NSString *dateStr =[NSString stringWithFormat:@"%@",oldDate];
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"YYYY-MM-dd HH:mm:ss +0000"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:@"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
[dtF release];
[dateFormatStr release];
return strDate;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *item = (NSDictionary *)[blogEntries
objectAtIndex:indexPath.row];
//cell.textLabel.text = [item objectForKey:@"a:DateIn"];
//NSString *dateString = @"2011-10-28T08:12:58.36+02:00";
NSString *dateString = [item objectForKey:@"a:DateIn"];
//To convert the string to NSDate
NSRange range = [dateString rangeOfString:@":" options:NSBackwardsSearch];
dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:range];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-ddEHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"MM:dd HH:mm"];
NSString *dateString1 = [dateFormat1 stringFromDate:date];
[dateFormat1 release];
//dateString1 is the required string.
cell.textLabel.text = dateString1;
cell.detailTextLabel.text = [item objectForKey:@"a:Description"];
cell.detailTextLabel.font=[UIFont fontWithName:@"Arial" size:12.0];
return cell;
}