2012-09-27 79 views
0

我有一個行爲不當的NSDateCategory。我設置的年,月的一天,但它的結果是完全不同的:NSDateComponents不接受輸入

變量: 年= 2012 月= 10 天= 1

+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day 
    { 
     NSCalendar *calendar = [NSCalendar currentCalendar]; 
     NSDateComponents *components = [[NSDateComponents alloc] init]; 
     [components setYear:year]; 
     [components setMonth:month]; 
     [components setDay:day]; 
     NSDate *result = [calendar dateFromComponents:components]; 

     return result; 
    } 

的NSLog輸出 結果= 2012 -09-30 23:00:00 +0000

回答

2

添加此行

[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 

希望這會爲你工作

0

做到這一點:

NSDateComponents *組件= [日曆組件:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit FROM日期:[NSDate的日期];