English 中文(简体)
日期 月刊
原标题:iOS dateFormatter dateFromString month issue

希望这只是一个快的阶段,我一米没有简单的东西......

I ve got an NSDateFormatter, which I m using to transformation the strong>2011-11-10 into a Annual Object.

NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:@"YYYY-MM-DD"];

// input of 2011-11-10, output of 2011-01-10 00:00:00 +0000        
[appointment setDate:[fmtDate dateFromString:
  [tempAppointment objectForKey:@"date"]
]];

从NSDateFormatter的返回存放在管理下的主题案文中。 我的问题是,日期是2011-01-10 00:00 +0000

为什么这个月从Nobvember减少到1月份? 它保留年金和日金,但不是每月。

我是否需要列入我储存日期的时间? 还是与我确定的形式有关?

最佳回答

如果使用YYYYY,你的日期说明是不正确的。 Try yyyyy-M-d

NSString *dateString = @"2011-11-10";

NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:@"yyyy-M-d"];

NSDate *date = [fmtDate dateFromString:dateString];
NSLog(@"date: %@", date);

产出:

2011-11-10 20:01:40.638 Craplet[81514:707] date: 2011-11-10 05:00:00 +0000

See:

http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns/a>

问题回答

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#/apple_ref/doc/uid/TP40002369-SW1"rel=“nofollow”>。 它有一例,将YYYYY列为常见错误。





相关问题
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, ...

热门标签