首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDateComponents不接受输入

NSDateComponents不接受输入
EN

Stack Overflow用户
提问于 2012-09-27 09:30:55
回答 2查看 39关注 0票数 0

我有一个行为不端的NSDateCategory。我正在设定年度、月份的日期,但结果完全不同:

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

代码语言:javascript
复制
 + (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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-27 09:38:25

添加这一行

代码语言:javascript
复制
[components setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

希望这对你有用

票数 2
EN

Stack Overflow用户

发布于 2012-09-27 10:21:33

做:

NSDateComponents* components =[日历- components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12618255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档