我怎样才能知道是否NSDate是营业日?也就是说,根据用户当前的区域设置和日历设置,它是否是一个周末 - 所以不是硬编码只是周一到周五?
NSCalendar有一个firstWeekday特性,但只是似乎是一个表象的东西; 这是周日在美国和周一在英国.
编辑:我不担心假期,我只想以与内置日历应用程序相同的方式遮盖日历的周末.
我听说NSCalendariOS7中引入了一个函数.
看了一下docs并找不到它.也不是OS X文档.
功能- (BOOL)isDate:(NSDate *)date1 equalToDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit即是同一天/同一周/月中的这两个日期等等......
但是,当我尝试在Xcode 5中使用它时.AutoComplete显示红色触发,然后当我实际使用它时,我收到此错误...

这很奇怪,因为功能很明显,我只是无法使用它.
任何人都可以阐明为什么会这样吗?
我是新来的NSCalander,NSdates和NSDateComponents
基本上我有一个本地通知,我想根据用户的选择重复开火日期,让我们说只在周日和周一.
我们应该使用repeatCalendar属性UILocalNotification但我无法达到如何设置它.
那么任何人都能用简单的代码行帮助我吗?
谢谢
我有一个用Swift编写的应用程序,它在iOS模拟器上运行良好.该项目的目标设置为iOS 7.1.
当我在iPhone 5s上运行应用程序时,它崩溃在以下行:
let calendar = NSCalendar(identifier: NSGregorianCalendar)
Run Code Online (Sandbox Code Playgroud)
有错误:
+[NSCalendar calendarWithIdentifier:]: unrecognized selector sent to class 0x1955eee60
Run Code Online (Sandbox Code Playgroud)
我是否必须针对iOS8与iOS7.x进行不同的调用?
我用Xcode 6创建我的应用程序,它在ios8上工作正常.我刚刚在ios7上测试了应用程序,我收到此错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSCalendar calendarWithIdentifier:]: unrecognized selector sent to class 0x3a78418c
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码,用于将日历从公历转换为波斯日历:
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:@"persian"];
Run Code Online (Sandbox Code Playgroud) 在tableviewcontroller中,我有这个代码来获取从0到6的天数,其中首先是当前dayNumber的顺序.
self.dayOrder=[NSMutableArray new];
NSCalendar *cal=[NSCalendar currentCalendar];
NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,
for (int i=0;i<7;i++) {
[self.dayOrder addObject:[NSNumber numberWithInteger:dayNumber]];
dayNumber=(dayNumber+1)%7;
}
Run Code Online (Sandbox Code Playgroud)
当我移动到此视图控制器时,我收到此错误 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSCopyOnWriteCalendarWrapper component:fromDate:]: unrecognized selector sent to instance 0x15e4b850'
当我添加一个ALL Exceptions断点时,此行发生错误
NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,
Run Code Online (Sandbox Code Playgroud)
它似乎正在我的iPhone 6上运行,但它在iOS 8上,而不是iOS7上的iPone 4,5c.
不知道为什么会这样,为什么会有任何帮助将不胜感激.
谢谢您的帮助!!!
我在数据库中查询startDate和endDate之间的值.因此,我正在寻找一种巧妙的方式来获得NSDate以结束一天的约会.就像你可以获得startOfDayForDate()一样.
我想你可以这样做:
let cal = NSCalendar.currentCalendar()
let startOfDay = cal.startOfDayForDate(NSDate())
let aDay:NSTimeInterval = 60*60*23 + 60*59 + 59
let endofDay = startOfDay.dateByAddingTimeInterval(aDay)
Run Code Online (Sandbox Code Playgroud)
将component.day + 1添加到startOfDayForDate是获取日期"当天结束"的正确方法,还是有更好的方法?
我有一个函数,它返回一个由月和年组成的周数数组.
这是代码:
let calendar = NSCalendar.currentCalendar()
let weekRange = calendar.rangeOfUnit(NSCalendarUnit.CalendarUnitWeekOfYear, inUnit: .CalendarUnitMonth, forDate: NSDate())
weekArray = Array(weekRange.location..<weekRange.location + weekRange.length)
Run Code Online (Sandbox Code Playgroud)
示例:对于2015-08-21,它将返回[31,32,33,34,35,36],这是正确的,因为该网站.
它工作正常,但我发现一个错误,如果我通过这个日期"2016-01-01"它返回[1,2,3,4,5],但正确的数组应该是[53(从2015年),1, 2,3,4](再次在本网站上查看).
如该网站所述:
一年中的第一周(WN 1)是包含1月4日或本年度第一个星期二的一周.
那么,我必须写一些计算才能使它有效吗?或者有一个更简单的解决方案?感谢帮助.
我有个问题.我想知道如何删除警告?谢谢
NSDate *fireDate = [picker_date date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMinute:-10];
NSDate *heure_notification = [gregorian dateByAddingComponents:offsetComponents toDate:fireDate options:0];
UILocalNotification *local_rappel_notification = [[UILocalNotification alloc]init];
local_rappel_notification.fireDate = heure_notification;
local_rappel_notification.alertBody = @"VOUS AVEZ UN VOL MAINTENANT DANS 10 MIN!!";
local_rappel_notification.timeZone = [NSTimeZone defaultTimeZone];
local_rappel_notification.soundName = UILocalNotificationDefaultSoundName;
local_rappel_notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication ]scheduleLocalNotification:local_rappel_notification];
Run Code Online (Sandbox Code Playgroud)
因此,我一直在研究如何确定iOS应用程序是否是新的一天。基本上,我希望新的一天开始时将一堆值重置为0。我已经看到可以使用“ UIApplicationSignificantTimeChangeNotification”在午夜发送通知。但是,如果用户在9:00 AM处终止了该应用程序并在第二天9:00 AM再次打开该应用程序,这还会触发吗?
我还看到我可以使用“ NSCalendarDayChangedNotification”,据我了解,该行为具有相同的行为。我唯一关心的是,即使在用户终止应用程序之后,这两个方法中的任何一个是否都能完成检测日期变化的操作。
理想情况下,我想将通知观察者添加到viewDidLoad的.m文件顶部,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetValues) name:UIApplicationSignificantTimeChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
然后在文件中的其他地方...
- (void)resetValues {
... // reset values to 0
}
Run Code Online (Sandbox Code Playgroud)
目标是在每天更改开始时触发此通知。上面的代码能实现我想要的行为吗?
nscalendar ×10
ios ×9
objective-c ×5
nsdate ×3
swift ×2
cal ×1
nslocale ×1
week-number ×1
xcode ×1