我正在做一些天气应用程序,因为我想显示4天的预测,即当前一天到下一个3天。我现在收到了当天的代码
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// 1 = Sunday, 2 = Monday, etc.
NSInteger day0 = comp.weekday;
NSLog(@"My DAY is %i", day0);
NSString *str;
NSMutableString *myString = [NSMutableString string];
str = [NSString stringWithFormat:@"%d",day0]; //%d or %i both is ok.
[myString appendString:str];
NSLog(@"My DAY is %@", myString);
输出是6,即星期五......那很酷
如何在没有增加当天的情况下获得第二天...我试图增加当天的日期,但问题是,如果我将当前日期改为7,那么它将增加到8没有这样的事情在weedays对..我是xcode的新手..帮帮我们......
答案 0 :(得分:0)
尝试以下方法:
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 1;
today = [calendar dateByAddingComponents:dayComponent toDate:today options:0];
答案 1 :(得分:0)
“如何在不增加当天的情况下第二天获得......”
如果你想要一个代表每一天的NSDate对象,你可以使用NSDateComponents来加起来:
NSDate *today = [NSDate date];
NSDateComponents *offset = [[NSDateComponents alloc] init];
offset.day = 1; // create a one day offset
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:today options:0];
然后得到你的工作日:
[[NSCalendar currentCalendar] components: NSWeekdayCalendarUnit fromDate:tomorrow];
“我试图增加当天的日期,但问题是,如果我将当前日期改为7,它会增加到8没有这样的事情在正常情况下......”
如果仅仅增加今天的工作日就足够了,可以使用一个简单的模数(这在任何编程语言中都是基本的):
int weekdayOfTomorrow = (++weekdayOfToday % 7) + 1;