代码之家  ›  专栏  ›  技术社区  ›  Raj Pawan Gumdal

NSDateFormatter在iOS 3.0中为@“dd-MM-yy”返回nil

  •  2
  • Raj Pawan Gumdal  · 技术社区  · 14 年前

    NSDate *date =nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:@"dd-MM-yy"];
    date = [dateFormatter dateFromString:inString];
    [dateFormatter release];
    

    它工作得非常好,正如iOS 4.0所期望的那样。但3.0中没有相同的代码。

    我得到的字符串类似于“12-Nov-10”,它包含在in string指针中。

    谢谢,

    拉吉

    编辑: 在Harkonian和Q&A讨论提出以下建议后,制定适当的准则:

    NSDate *date =nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:@"dd-MMM-yy"];
    
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [enUSPOSIXLocale release];
    
    date = [dateFormatter dateFromString:inString];
    [dateFormatter release];
    
    2 回复  |  直到 14 年前
        1
  •  7
  •   memmons    13 年前

    如果使用的是用户可见的日期,则应避免设置日期格式字符串,因为很难预测在所有可能的用户配置中如何表示格式字符串。相反,您应该尝试并限制自己设置日期和时间样式(通过-[NSDateFormatter setDateStyle:]和-[NSDateFormatter setTimeStyle:])。

    另一方面,如果使用的是固定格式的日期,则应首先将日期格式化程序的区域设置设置为适合固定格式的设置。在大多数情况下,最好选择的语言环境是“en_US_POSIX”,这是一种专门设计的语言环境,无论用户和系统偏好如何,都能产生美式英语结果enòUSòPOSIX“在时间上也是不变的(如果美国在未来某个时候改变了它格式化日期的方式,“enòUS”将改变以反映新的行为,但“enòUSòPOSIX”不会改变),在机器之间(“enòUSòPOSIX”在iPhone操作系统上的工作方式与在Mac OS X上的相同,在其他平台上也一样)。

    一旦您将“en_US_POSIX”设置为日期格式化程序的区域设置,您就可以设置日期格式字符串,并且日期格式化程序将为所有用户一致地工作。

    上面的信息和更多可以在苹果的 Technical Q&A QA1480

    以下是我的应用程序中实现上述建议的代码片段:

    static NSDateFormatter* dateFormatter = nil;
    if (!dateFormatter) {
       dateFormatter = [[NSDateFormatter alloc] init];
    
       NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] 
          initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
       assert(enUSPOSIXLocale != nil);
       [dateFormatter setLocale:enUSPOSIXLocale];
       [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
       dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
    }
    
        2
  •  10
  •   Raj Pawan Gumdal    10 年前

    a:  AM/PM
    A:  0~86399999 (Millisecond of Day)
    
    c/cc:   1~7 (Day of Week)
    ccc:    Sun/Mon/Tue/Wed/Thu/Fri/Sat
    cccc:   Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
    
    d:  1~31 (0 padded Day of Month)
    D:  1~366 (0 padded Day of Year)
    
    e:  1~7 (0 padded Day of Week)
    E~EEE:  Sun/Mon/Tue/Wed/Thu/Fri/Sat
    EEEE:   Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
    
    F:  1~5 (0 padded Week of Month, first day of week = Monday)
    
    g:  Julian Day Number (number of days since 4713 BC January 1)
    G~GGG:  BC/AD (Era Designator Abbreviated)
    GGGG:   Before Christ/Anno Domini
    
    h:  1~12 (0 padded Hour (12hr))
    H:  0~23 (0 padded Hour (24hr))
    
    k:  1~24 (0 padded Hour (24hr)
    K:  0~11 (0 padded Hour (12hr))
    
    L/LL:   1~12 (0 padded Month)
    LLL:    Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    LLLL:   January/February/March/April/May/June/July/August/September/October/November/December
    
    m:  0~59 (0 padded Minute)
    M/MM:   1~12 (0 padded Month)
    MMM:    Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    MMMM:   January/February/March/April/May/June/July/August/September/October/November/December
    
    q/qq:   1~4 (0 padded Quarter)
    qqq:    Q1/Q2/Q3/Q4
    qqqq:   1st quarter/2nd quarter/3rd quarter/4th quarter
    Q/QQ:   1~4 (0 padded Quarter)
    QQQ:    Q1/Q2/Q3/Q4
    QQQQ:   1st quarter/2nd quarter/3rd quarter/4th quarter
    
    s:  0~59 (0 padded Second)
    S:  (rounded Sub-Second)
    
    u:  (0 padded Year)
    
    v~vvv:  (General GMT Timezone Abbreviation)
    vvvv:   (General GMT Timezone Name)
    
    w:  1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
    W:  1~5 (0 padded Week of Month, 1st day of week = Sunday)
    
    y/yyyy: (Full Year)
    yy/yyy: (2 Digits Year)
    Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
    YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)
    
    z~zzz:  (Specific GMT Timezone Abbreviation)
    zzzz:   (Specific GMT Timezone Name)
    Z:  +0000 (RFC 822 Timezone)