English 中文(简体)
使用NSDateFormatter的Im有什么错误?
原标题:What s wrong with how I m using NSDateFormatter?
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
   [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
   NSString *dateString = @"Tue, 08 Jun 2010 17:00:00 EDT";
   NSDate *eventDate = [dateFormatter dateFromString:dateString];

在本案中 日期物体为零。 有些人能否把我cl在一边? 该法典已投入使用。

UPDATE: Can t talk about why this doesn t work due to NDA. Suffice it to say, when iOS 4 is out I will post the answer to my own question.

最佳回答

The answer to this question is the following: I was using the wrong date format string:

@"EEE, d MMM yyyy HH:mm:ss zzz"

应:

@"EEE, dd MMM y HH:mm:ss zzz"

关于OS4和NDA的部分,我认为我必须使用方法dateFormatFromTemplate:options: locale:。 这样做是为了:

NSString *format = [NSDateFormatter dateFormatFromTemplate:@"EEE, d MMM yyyy HH:mm:ss zzz" options:0 locale:[NSLocale currentLocale]];

然而,只有当你想把这个方法送到一个当地人身份不明的用户时,才能使用这种方法。 在我的案件中,我确切地知道这一日期的形式将如何看待,我正试图把这个日期放在核心数据库中。 因此,这种方法没有用处。

<<>Bonus bookmark: 非常仔细地阅读这一表格,你肯定会说明问题是什么。 统一编码日期格式应当遵循这些规格:

<<>strong>TL;DR 形式说明是错误的。 D oh!

问题回答
/*
    x           number
    xx          two digit number
    xxx         abbreviated name
    xxxx        full name

    a           AM/PM
    A           millisecond of day
    c           day of week (c,cc,ccc,cccc)
    d           day of month
    e           day of week (e,EEE,EEEE)
    F           week of month
    g           julian day (since 1/1/4713 BC)
    G           era designator (G=GGG,GGGG)
    h           hour (1-12, zero padded)
    H           hour (0-23, zero padded)
    L           month of year (L,LL,LLL,LLLL)
    m           minute of hour (0-59, zero padded)
    M           month of year (M,MM,MMM,MMMM)
    Q           quarter of year (Q,QQ,QQQ,QQQQ)
    s           seconds of minute (0-59, zero padded)
    S           fraction of second
    u           zero padded year
    v           general timezone (v=vvv,vvvv)
    w           week of year (0-53, zero padded)
    y           year (y,yy,yyyy)
    z           specific timezone (z=zzz,zzzz)
    Z           timezone offset +0000

    sql         y-M-d H:m:s
    rss         [E, ]d MMM y[y] H:m:s Z|z[zzz]
*/

这是我迄今为止的评论。 我使用了以下文字:toDateUsingFormat, 使用了NSDateFormatter, 载体上通过。 我不使用当地人,因为日期没有地方化。

    if ( 0 == [string rangeOfString:@","].length ) {
        result = [string toDateUsingFormat:@"d MMM y H:m:s z"];
    } else {
        result = [string toDateUsingFormat:@"E, d MMM y H:m:s z"];
    }

Edit:

缩略语

NSDate *result = nil;
NSError *error = nil;
[dataFormatter getObjectValue:&result forString:dateString errorDescription:&error];

The problem that I found is that the string that I was parsing has some trailing characters " ". The solution was to remove them:

[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

你在座标上有一个零添加日,即08,然而,以你的格式说明格式,你试图将一个非零添加日,即<代码>d。 更改<代码>d至d 这个问题应该解决。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签