【问题标题】:NSDateFormatter problem with increased Date增加日期的 NSDateFormatter 问题
【发布时间】:2010-07-01 09:18:34
【问题描述】:

我正在尝试增加日期并获得每个月的格式化输出... 它工作正常,但 2010 年 12 月至 2011 年 1 月的步骤不起作用。 请看看这个:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *lastDate = [[NSDate alloc] init];

for (int i = 0; i < 12; i++) {
    NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
    [offsetComponents setMonth: 1];
    lastDate = [[gregorian dateByAddingComponents:offsetComponents toDate: lastDate options:0] retain];


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateFormat:@"MMMM YYYY"];

    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    [dateFormatter setLocale:locale];

    NSLog(@"%d: Fromatted Date: %@ from : %@", i, [dateFormatter stringFromDate:lastDate], lastDate);
}

调试输出: [会议开始于 2010-07-01 11:05:18 +0200。] 0: 起始日期: 2010 年 8 月起始于 : 2010-08-01 11:05:19 +0200

1: Fromatted Date: 2010 年 9 月 从 : 2010-09-01 11:05:19 +0200

2: Fromatted Date: 2010 年 10 月 from : 2010-10-01 11:05:19 +0200

3: 起始日期: 2010 年 11 月 起始日期: 2010-11-01 11:05:19 +0100 4:自成立日期:2010 年 12 月起:2010-12-01 11:05:19 +0100

5: Fromatted Date: January 2010 from : 2011-01-01 11:05:19 +0100

6: 起始日期:2011 年 2 月起始日期:2011-02-01 11:05:19 +0100

7: Fromatted Date: 2011 年 3 月 来自 : 2011-03-01 11:05:19 +0100

8: Fromatted Date: April 2011 from : 2011-04-01 11:05:19 +0200

9: Fromatted 日期:2011 年 5 月 来自:2011-05-01 11:05:19 +0200

10: Fromatted Date: 2011 年 6 月 from : 2011-06-01 11:05:19 +0200

11: Fromatted Date: 2011 年 7 月 from : 2011-07-01 11:05:19 +0200

【问题讨论】:

    标签: iphone objective-c xcode nsdateformatter


    【解决方案1】:

    嗯 :-( 真的很奇怪。我刚刚制作了一个新的基于视图的应用程序... 找不到提示

    **更新:好的,如果我使用 [dateFormatter setDateFormat:@"MMMM yyyy"] (所以只是小写 Y)它的工作原理。

    y/yyyy:(全年)

    yy/yyy:(2 位数年份)

    Y/YYYY:(全年,从一年中第一周的星期日开始)

    YY/YYY:(2 位数年份,从一年中第一周的星期日开始)

    【讨论】:

    • 这绝对是答案。将 YYYY 替换为 yyyy - 现在一切正常。
    • 谢谢;这也解决了我的问题! :-)
    【解决方案2】:

    我也没有遇到你所说的任何问题。 我还复制了您的代码并且工作正常。

    我不确定,但我认为 SDK 版本肯定有问题。因为我们使用的是 3.1.3 并且工作正常。只需尝试不同的 sdk 版本。

    米图尔干杯

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多