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

この場合、eventDateオブジェクトはnilです。誰かが私を手がかりにできますか?このコードは以前は機能していました。

更新:NDAが原因でこれが機能しない理由について話すことはできません。iOS 4がリリースされたら、私自身の質問に対する答えを投稿します。

4

5 に答える 5

64
/*
    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を使用します。NSDateFormatterrssの日付がローカライズされていないため、ロケールは使用しません。

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

編集:

getObjectValue:の代わりに使用しdateFromStringます。

NSDate *result = nil;
NSError *error = nil;
[dataFormatter getObjectValue:&result forString:dateString errorDescription:&error];
于 2010-06-07T22:26:48.413 に答える
4

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

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

when it should have been:

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

The part about iOS 4 and NDA was that I thought I had to use the NSDateFormatter method dateFormatFromTemplate:options:locale: which would have looked like this:

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

However, that method should only be used when you want to DISPLAY the date to a user of unknown locale. In my case, I knew exactly what the date format was going to look like and I was trying to PARSE the date string so that I could store it in CoreData. Therefore, that method wasn't useful.

Bonus bookmark: Read this table very carefully and you will definitely figure out what the problem is... Unicode date formats should follow these specifications: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table

TL;DR The format string was wrong. D'oh!

于 2011-08-11T20:46:38.977 に答える
3

フォーマット指定子の完全なリストは、UTS#35日付フォーマットパターンです。

「E」の代わりに「c」文字は機能しますか?このドキュメントには非常に近い代替手段として含まれており、希望する結果が得られる場合があります。

(テーブルにないフォーマット文字列の文字が本当に必要な場合は、エスケープできます。たとえば、hh 'o''clock' a, zzzz「12時PM、太平洋夏時間」のようなフォーマットを生成します。)

于 2010-06-07T23:06:06.063 に答える
0

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

[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

于 2010-07-24T04:41:14.177 に答える
0

you have a zero padded day, namely 08 in your date string, however in your format string the format is trying to parse a non-zero padded day, namely d. changing d to dd should fix the problem

于 2013-03-01T17:23:55.757 に答える