NSDateFormatter 为 ios 4.3.3 中的日期返回 null [英] NSDateFormatter is returning null for date in ios 4.3.3

查看:30
本文介绍了NSDateFormatter 为 ios 4.3.3 中的日期返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将字符串转换为日期并再次转换为字符串.使用以下代码

I am converting string to date and again to string. using following code

 NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];


    NSLog(@"New Date is %@",newDate);
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy h:mm:ss a"];
    NSDate *myDate = [dateFormat dateFromString:newDate];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];

    NSString *str = [dateFormat stringFromDate:myDate];

    NSLog(@"DAte is %@",str);

它在模拟器中返回日期,但在 iphone 4.3.3 中没有请帮忙

It is returning date in simulator but not in iphone 4.3.3 please help

推荐答案

NSDateFormatter 如果无法从字符串中获取日期,将返回 nil.要查看它是否可以解析日期,请添加其他 NSLog 语句:

NSDateFormatter will return nil if it can't get the date from a string. To see if it can parse the date add an other NSLog statement:

NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];

NSLog(@"New Date is %@",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm:ss a"];

NSDate *myDate = [dateFormat dateFromString:newDate];
NSLog(@"Date parsed: %@", myDate);

[dateFormat setDateFormat:@"MM/dd/yyyy"];

NSString *str = [dateFormat stringFromDate:myDate];

NSLog(@"DAte is %@",str);

但是既然你只想要字符串的第一部分,为什么不直接将它子串起来.

But since you only want the firs part of the string why not just substring it.

NSString *str = [newDate substringToIndex:10];

以及这里返回的内容:

NSString *newDate =[NSString stringWithFormat:@"%@",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];

它的属性 Date 已经是一个 NSDate 吗?,你能显示你的 .h 文件吗?

it the property Date already a NSDate ?, can you show your .h file for Jobs?

这篇关于NSDateFormatter 为 ios 4.3.3 中的日期返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆