xcode NSDateFormatter 在configureView 中返回null

发布于 2025-01-04 21:07:36 字数 1855 浏览 3 评论 0原文

我正在尝试将 NSDateFormatter 与以下代码一起使用。这是我的 DetailViewController.m 中的代码。我正在使用带有情节提要和核心数据的 Xcode 4.2。我在这里搜索了 NSDateFormatter 的其他主题,似乎我做得正确,但结果返回 null。

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {

        // Format Date to MMM D, YYYY H:MM:SS AM/PM
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];

        NSString *sessDate = [[self.detailItem sessionDate] description];

        NSDate *date = [dateFormatter dateFromString:sessDate];
        NSString *formattedDate = [dateFormatter stringFromDate:date];

        self.sessionDate.text = formattedDate;

        NSLog(@"sessDate = %@",sessDate);
        NSLog(@"date = %@",date);
        NSLog(@"formattedDate = %@",formattedDate);
    }
}

NSLog 结果:

2012-02-10 17:18:17.781 ClickIt[332:10103] sessDate = 2012-02-09 17:11:33 +0000
2012-02-10 17:18:17.782 ClickIt[332:10103] date = (null)
2012-02-10 17:18:17.782 ClickIt[332:10103] formattedDate = (null)

我以前使用过 NSDateFormatter,它工作得很好,但我很困惑为什么日期变量返回 null。对我做错了什么有什么想法吗?

谢谢!


调整后的代码

    // Format Date to MMM D, YYYY H:MM:SS AM/PM
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSString *sessDate = [[self.detailItem sessionDate] description];
    NSDate *date = [dateFormatter dateFromString:sessDate];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    self.sessionDate.text = formattedDate;

I'm trying to use NSDateFormatter with the following code. This is the code within my DetailViewController.m. I'm using Xcode 4.2 with Storyboards and Core Data. I've searched other topics here for NSDateFormatter and it seems as though I'm doing this correctly but the results return null.

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {

        // Format Date to MMM D, YYYY H:MM:SS AM/PM
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];

        NSString *sessDate = [[self.detailItem sessionDate] description];

        NSDate *date = [dateFormatter dateFromString:sessDate];
        NSString *formattedDate = [dateFormatter stringFromDate:date];

        self.sessionDate.text = formattedDate;

        NSLog(@"sessDate = %@",sessDate);
        NSLog(@"date = %@",date);
        NSLog(@"formattedDate = %@",formattedDate);
    }
}

NSLog results:

2012-02-10 17:18:17.781 ClickIt[332:10103] sessDate = 2012-02-09 17:11:33 +0000
2012-02-10 17:18:17.782 ClickIt[332:10103] date = (null)
2012-02-10 17:18:17.782 ClickIt[332:10103] formattedDate = (null)

I've used NSDateFormatter before and it worked fine but I'm stumped as to why the date variable is returning null. Any thoughts as to what I'm doing wrong?

Thanks!


adjusted code

    // Format Date to MMM D, YYYY H:MM:SS AM/PM
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSString *sessDate = [[self.detailItem sessionDate] description];
    NSDate *date = [dateFormatter dateFromString:sessDate];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    self.sessionDate.text = formattedDate;

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

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

发布评论

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

评论(1

海未深 2025-01-11 21:07:36

我认为 NSDateFormatterMediumStyle 对应于这样的内容 - 1937 年 11 月 23 日。因此您得到的是 null 值。

你能试试这个吗?

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

I think NSDateFormatterMediumStyle corresponds to something like this - Nov 23, 1937. Hence you are getting null value.

Can you try this?

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文