English 中文(简体)
按年份和月份分列的NSDFormatter格式
原标题:NSDateFormatter format date by year and month

我试图将国家可持续发展论坛定为一年。

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
NSLog(@"**> %@",[dateFormatter dateFromString:@"2012"]);
[dateFormatter release];

但它的印刷:**>2011-12-31:00 +0000

也许有人会帮助我了解这一点。

最佳回答

这必须涉及UTC(GMT)与贵时区之间的时间区差异(可能位于GMT+2)。 NSDate is in UTC byault. 因此[NSDate Day]可以偏离当地时间。

The following code should output Jan 1, 2012:

NSDateF或matter *dateF或matter = [[NSDateF或matter alloc] init];
    [dateF或matter setDateF或mat:@"yyyy zzz"];
NSLog(@"**> %@",[dateF或matter dateFromString:@"2012 GMT"]);
[dateF或matter release];

NSDateF或matter *dateF或matter = [[NSDateF或matter alloc] init];
[dateF或matter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateF或matter setDateF或mat:@"yyyy"];
NSLog(@"NSDate : %@",[dateF或matter dateFromString:@"2012"]);
[dateF或matter release];
问题回答

希望来自+02:00时区。 由于NSDate总是给GMT区的时间,而不是印刷2012-01 00:00:00 +0000/code>,因此正在印刷GMTzone时间的上述数值。

<><>Edit>:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy"];
NSLog(@"NSDate : %@",[dateFormatter dateFromString:@"2012"]);
[dateFormatter release];




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