日期不会出现在UILabel中 [英] Date does not appear in UILabel

查看:126
本文介绍了日期不会出现在UILabel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有什么问题?

    NSString *strTest = @"Sun Apr 12 23:29:24 +0000 2009";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yy"];
    NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
    self.createdAt.text = [dateFormatter stringFromDate:createdAtFormatted];

self.createdAt是UILabel,它保持空白。为什么?

self.createdAt is a UILabel and it stays empty. Why?

当我尝试使用dateWithNaturalLanguageString时,我得到:

When trying to use dateWithNaturalLanguageString, I get:

推荐答案

您的日期格式化日期格式与 strTest 字符串中的日期格式不匹配。

Your date formatter date format does not match the date format from the strTest string.

NSString *strTest = @"Sun Apr 12 23:29:24 +0000 2009";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // so the formatter recognizes english names for months and days
[dateFormatter setLocale:loc];
[loc release]; // remove if using ARC
[dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSDate *createdAtFormatted = [dateFormatter dateFromString:strTest];
NSLog(@"got %@", createdAtFormatted);

可以找到格式化程序说明符此处

The formatter specifiers can be found here.

这篇关于日期不会出现在UILabel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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