【问题标题】:Formatting Date to dd-MMM in iOS在 iOS 中将日期格式化为 dd-MMM
【发布时间】:2012-05-13 18:40:41
【问题描述】:

我一直在搜索这个,但找不到它。我在包含 NSDate 的 NSDictionary 中有一个对象。现在标准的 NSDate 对象已经很长了。我想以 dd-MMM 格式向用户展示。

例如:原始日期可能是2012-04-23 00:00:00 +0000,我希望日期显示为23 Apr

我尝试使用 NSDateComponents 和 NSDateFormatter,但效果不佳。可能我没有弄清楚所需格式的正确用法。

为了更好的理解,下面是代码:

            NSLog(@"From Dictionary, Date = %@",[tmpDict objectForKey:@"eventDate"]);
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss Z"]; 
            NSString *dateString = [tmpDict objectForKey: @"eventDate"];
            NSDate *date = [dateFormatter dateFromString: dateString];

            NSLog(@"MY DATE: %@",date);
            NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
            [dateFormatter2 setDateFormat:@"dd-MMM"];
            NSString *formattedDateString = [dateFormatter2 stringFromDate:date];
            NSLog(@"Formatted Date: %@",formattedDateString);

From Dictionary, Date 的输出为 2012-05-19 07:30:56,格式化日期和我的日期为空。 tmpDict 是我正在使用的字典对象。

提前感谢您的帮助。

【问题讨论】:

    标签: iphone objective-c ios nsdate nsdateformatter


    【解决方案1】:

    根据Duncan C的回答。

    如果将日期表示为字符串,则先将其转为日期,然后在其上使用 NSDateFormatter。假设格式将保持不变,它将是

    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss Z"]; 
    NSString *dateString = [dictionaryObject objectForKey: @"eventDate"];
    NSDate *date = [dateFormatter dateFromString: dateString];
    

    然后你就可以对date对象做你现在正在做的事情了。

    【讨论】:

    • 我尝试了您的建议,但仍然无效。我将编辑问题以反映尝试过的代码。
    • 问题在于你在字符串中的日期格式,你说它应该是 2012-04-23 00:00:00 +0000,但实际上是 2012-04-23 00 :00:00。您必须始终确保它只是两者之一,并相应地调整 dateFormatter 格式。
    【解决方案2】:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MMM"];
    NSString *formattedDateString = [dateFormatter stringFromDate:myDate];
    [dateFormatter release];
    

    【讨论】:

    • 它不起作用。 NSDate *myDate=[tmpDict objectForKey:@"eventDate"]; NSLog(@"我的日期:%@",myDate); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MMM"]; NSString *formattedDateString = [dateFormatter stringFromDate:myDate]; NSLog(@"格式化日期:%@",formattedDateString);我在 MY DATE 中得到输出,但在 formattedDateString 中没有。我在那里得到一个 NULL。
    • 第一个日志的输出是什么?
    • MY DATE: 2012-05-19 07:30:56 格式化日期: (null)
    • 你可以试试 NSLog(@"MY DATE: %@", [myDate description]); ?
    • MY DATE: 2012-05-19 07:30:56 这是 [myDate description] 的输出。
    【解决方案3】:

    听起来您使用 key eventDate 从字典中获取的对象是字符串,而不是日期。

    试试这个代码:

    NSDate *myDate=[tmpDict objectForKey:@"eventDate"]; 
    NSLog(@"myDate class = %@", [myDate class]);
    

    我敢打赌,它显示的是一个 NSString 类或其子类,而不是 NSDate。

    【讨论】:

    • 是的..它显示 __NSCFString 将其引用到 NSString。有什么解决方法吗?
    • 您需要创建 2 个日期格式化程序。我们称它们为 inputDateFormatter 和 outputDateFormatter。 inputDateFormatter 将设置为接受传入日期格式的日期字符串并将其转换为 NSDate。然后,您将获取生成的 NSDate 并使用 outputDateFormatter 将其转换为您的输出格式。我不是日期格式化程序方面的专家,并且总是需要翻阅文档才能弄清楚如何处理特定格式的日期。
    • 好吧..我也试过了,还是不行。我已更新问题以反映更改。不过,感谢您的支持。非常感谢。
    猜你喜欢
    • 2021-07-20
    • 2021-10-07
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多