NSDateFormatter 在 ios 4.3.3 中为日期返回 null

发布于 2024-12-04 01:15:11 字数 571 浏览 4 评论 0原文

我正在将字符串转换为日期,然后再次转换为字符串。使用以下代码

 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 中则不然 请帮忙

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-12-11 01:15:11

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

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);

但是既然您只想要字符串的第一部分,为什么不直接将其子字符串化。

NSString *str = [newDate substringToIndex:10];

这里返回什么:

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

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

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

and what is returned here:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文