NSDateFormatter 增加日期的问题

发布于 2024-09-07 14:34:49 字数 1800 浏览 0 评论 0原文

我试图增加日期并获得每个月的格式化输出...... 它工作正常,但 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: Fromatted 日期: 2010 年 8 月 从 : 2010-08-01 11:05:19 +0200

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

2: Fromatted 日期: 2010 年 10 月: 2010-10-01 11:05:19 +0200

3: Fromatted 日期: 2010 年 11 月 来自 : 2010-11-01 11:05:19 +0100 4: Fromatted 日期: 2010 年 12 月 from : 2010-12-01 11:05:19 +0100

5: Fromatted 日期: 2010 年 1 月 from : 2011-01-01 11:05:19 +0100 <--- 为什么是2010 年?!

6: Fromatted 日期: 2011 年 2 月 来源: 2011-02-01 11:05:19 +0100

7: Fromatted 日期: 2011 年 3 月 来源: 2011-03-01 11:05:19 +0100

8: Fromatted 日期: 2011 年 4 月: 2011-04-01 11:05:19 +0200

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

10: Fromatted 日期: 2011 年 6 月 来源 : 2011-06-01 11: 05:19 +0200

11:来自格式化日期:2011 年 7 月来自:2011-07-01 11:05:19 +0200

im trying to increase a date and get a formatted output for every month ...
it works fine, but the step Dec 2010 to Jan 2011 doesn't work.
please have a look at this:

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);
}

debug output:
[Session started at 2010-07-01 11:05:18 +0200.]
0: Fromatted Date: August 2010 from : 2010-08-01 11:05:19 +0200

1: Fromatted Date: September 2010 from : 2010-09-01 11:05:19 +0200

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

3: Fromatted Date: November 2010 from : 2010-11-01 11:05:19 +0100
4: Fromatted Date: December 2010 from : 2010-12-01 11:05:19 +0100

5: Fromatted Date: January 2010 from : 2011-01-01 11:05:19 +0100 <--- WHY is the year 2010 ?!

6: Fromatted Date: February 2011 from : 2011-02-01 11:05:19 +0100

7: Fromatted Date: March 2011 from : 2011-03-01 11:05:19 +0100

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

9: Fromatted Date: May 2011 from : 2011-05-01 11:05:19 +0200

10: Fromatted Date: June 2011 from : 2011-06-01 11:05:19 +0200

11: Fromatted Date: July 2011 from : 2011-07-01 11:05:19 +0200

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜血缘 2024-09-14 14:34:49

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

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

y/yyyy:(全年)

yy/yyy:(2 位数字年份)

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

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

hmm :-(
really strange. i just made up a new view-based app ...
cann't find a hint

**update: ok if i use [dateFormatter setDateFormat: @"MMMM yyyy"]
(so just lowercase Y) its working.

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)

为人所爱 2024-09-14 14:34:49

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

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

干杯米图尔

I also didn't face any problem which you are talking about.
I also copy your code and was working fine.

I am not sure but i think there must be a problem of SDK versions. because we are using 3.1.3 and was working fine. just try in different sdk versions.

Cheers Mitul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文