首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDate转换设置工作日

问NSDate转换设置工作日
EN

Stack Overflow用户
提问于 2012-11-26 13:49:34
回答 2查看 620关注 0票数 1

我有一个NSDate对象,*myDate.I可以从此日期获取星期几,我想将工作日设置为integer c,并仅在该周获取新的日期对象。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-26 14:18:15

代码语言:javascript
复制
 NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
            [formatter setDateFormat:@"MMM dd, yyy"];
            date = [formatter dateFromString:string];

            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |       NSWeekdayCalendarUnit;
            NSDateComponents *components = [calendar components:units fromDate:date];
            NSInteger day = [components day];
            NSInteger weekday = [components weekday]; // if necessary
票数 1
EN

Stack Overflow用户

发布于 2012-11-26 14:58:00

我搜索了一下,找到了答案,这是代码..

代码语言:javascript
复制
  NSDateComponents *comps = [[NSDateComponents alloc] init];
                [comps setDay:1];
             NSDate   *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
                NSLog(@"new date==%@",newDate);

这将返回第二天的日期。

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

https://stackoverflow.com/questions/13559314

复制
相关文章

相似问题

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