我想使用周末的日期在NSLocalNotification中使用它,但我不知道如何获取它,我试图用数学方法来做,但有时候我得到的数字大于本月的日子。如何获取周末的日期?
回答
你有关获取日期第一个问题就可以解决的:
NSCalendar *calender=[NSCalendar currentCalendar];
NSRange daysRange=[calender rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
NSUInteger numberOfDaysInMonth=daysRange.length;
//NSLog(@"num of days in current month : %ld",numberOfDaysInMonth);
NSDateComponents *dateComponents = [calender components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSInteger dayCount=[dateComponents weekday];
//if today itself is saturday what you want to display? today or upcoming one... then do small changes here, for sat & sun.
NSInteger daysForSaturday=7-dayCount;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd"];
NSUInteger todaysDate = [[formatter stringFromDate:[NSDate date]]integerValue];
// NSLog(@"Today is : %@",todaysDate);
NSUInteger comingSaturday=todaysDate+daysForSaturday;
if (comingSaturday>numberOfDaysInMonth) {
comingSaturday-=numberOfDaysInMonth;
}
NSUInteger comingSunday=comingSaturday+1;
NSLog(@"Coming.. Sat is : %ld, Sun in : %ld",comingSaturday, comingSunday);
这就是我所做的,但你可能会得到一天中的天数大于一个月中的天数 –
:(我坚持尝试这种情况...等待我会发布正确的结果,即使月变化和即使是年份的变化...... –
@ZiadTamim:请检查这段代码,我已经检查了这段代码的下列情况:1.考虑自动转移到下个月2.对于闰年如果你仍然发现一些错误,让我知道.. :) –
需要注意的是IOS支持多种日历,我不知道,如果使用这些日历的所有文化都有周末,如果一个概念,他们总是意味着有两天。
你还需要处理一些事情:即使在使用格里历的国家,一周也可能从星期一或星期日开始。
但是,如果我们假设一个周末等同于周六和周日,这可能对你有所帮助:
NSDate *referenceDate = [NSDate date];
NSDate *startOfThisWeek;
NSDate *saturday;
NSUInteger backupStartWeekday = [[NSCalendar currentCalendar] firstWeekday];
[[NSCalendar currentCalendar] setFirstWeekday:1]; // ensure week begins at sunday
[[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfThisWeek
interval:NULL
forDate:referenceDate];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit].length; //the start of the next week
components.day = components.day - 2;
saturday = [[NSCalendar currentCalendar] dateByAddingComponents:components
toDate:startOfThisWeek
options:0];
[[NSCalendar currentCalendar] setFirstWeekday:backupStartWeekday];
- 1. 获取日期到周末
- 2. 在php中获取周末日期
- 3. 获取下周日的日期(跳过周末)在HTML
- 4. 如何在SQL中获取当年的所有周末日期?
- 5. 如何获取给定周数和年份的首末日期?
- 6. 确定日期是周日/周末java
- 7. 如何在任何给定的日期在本周获得周末日?
- 8. 如果输入是日期和周末日(周日,周一,周三,周三,周四,周五或周四),如何计算周末日期
- 9. SQL:如何获取周开始日期和周结束日期
- 10. Javascript“周末”日期计算?
- 11. 我如何获得日期范围内的周末天数
- 12. SQL Server:周末以周五和上周末为结束日期
- 13. 获取下5个日期根据数据的最后日期排除周末
- 14. 获取昨天的日期,如果星期一得到周末范围
- 15. 获取天数两个日期之间不包括周末
- 16. 获取剩余日期,不包括周末
- 17. MySQL:如何使用当前日期获取“本周的日期”?
- 18. Excel - 如何获取日期是一周中的特定日期?
- 19. 如何获取本周的开始日期和结束日期?
- 20. javascript如何获取下周的日期和比较日期
- 21. SQL动态周末/ 3日日期
- 22. 获取日期与周
- 23. 如何在javascript中获取周末?
- 24. SQL更换日期值,如果日期是假期或周末
- 25. 将数据帧日期行转换为周末/非周末值
- 26. java日历 - 获取最后5天的一周(没有周末)
- 27. JavaScript:获取周日过去的日期
- 28. 如何在java中的两个日期之间获得除周末或周六或周日外的每一天?
- 29. 周末日历
- 30. 不包括日期时间的周末
我怕你的描述不足以明白你想要什么。但看到NSCalendar - 它会给你关于日期的各种信息。 –
请提供您所指的那个不起作用的代码,并告诉我们您期望它做什么。这样我们可以帮助你做到最好。我最好的猜测是你想从当前日期确定下一个星期六? – lnafziger
试试这个http://www.roseindia.net/tutorial/iphone/examples/Current-Date-iPhone-SDK.html,它可以解决你的问题。 – Girish