Xcode 使用NSDate和NSDate组件计算两个日期之间的时间(未来最多6天23小时59分钟59秒)
更新:这是一个工作示例 首先,我们创建一个类来保存工作日、小时、分钟和秒: myClass.hXcode 使用NSDate和NSDate组件计算两个日期之间的时间(未来最多6天23小时59分钟59秒),xcode,nsdate,nscalendar,nsdatecomponents,ekeventkit,Xcode,Nsdate,Nscalendar,Nsdatecomponents,Ekeventkit,更新:这是一个工作示例 首先,我们创建一个类来保存工作日、小时、分钟和秒: myClass.h #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface myClass : NSObject { NSString *weekday; NSInteger hour; NSInteger minute; NSInteger second; } @property (nonatomic, s
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface myClass : NSObject {
NSString *weekday;
NSInteger hour;
NSInteger minute;
NSInteger second;
}
@property (nonatomic, strong) NSString *weekday;
@property (nonatomic, assign) NSInteger hour;
@property (nonatomic, assign) NSInteger minute;
@property (nonatomic, assign) NSInteger second;
@end
接下来,我们需要创建一个保存日期信息的myClass实例
将其添加到ViewController.h:
@property (nonatomic, strong) NSMutableArray *myArray;
此代码将放在ViewController.m中的任意位置:
myArray = [[NSMutableArray alloc] init];
//Setup an instance of myClass
myClass *c = [[myClass alloc] init];
[c setWeekday:@"Monday"];
[c setHour:13];
[c setMinute:0];
[c setSecond:0];
[myArray addObject:c];
接下来,我们需要弄清楚我们的活动还有多远。多亏了rdelmar,我们有了这样做的代码,他的答案如下
//Create a func that returns an NSDate. It requires that the Weekday, HR, Min and Secs are passed into it.
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
//Setup an array of weekdays to compare to the imported (NSString *)weekday
NSArray *array = [NSArray arrayWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
//This code finds how many days in the future the imported (NSString *)weekday is
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
//Lastly, create an NSDate called eventDate that consists of the
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
return eventDate;
}
在这里,我们获取新创建的eventDate并使用它在iCal中创建我们的事件:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Move your car!";
event.startDate = eventDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:60.0f * 60.0f sinceDate:event.startDate]; //1 hr long
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -30.0f]]; //30 min before
//eventLoc was created using CLGeocoder and the method reverseGeocodeLocation:
//The location is not necessary to create an event but if you'd like the code, ask and i'll post it.
[event setLocation:eventLoc];
[event setNotes:@"This event was set by me. ;P"];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"Event Set");
我希望这能像帮助我一样帮助别人
:更新结束:
我已经阅读了NSDate的文档,希望找到一种简单的方法来查找“下一个即将到来的周一下午1:00”
例如,假设面包店每周1天(星期四)从上午9点到下午6点营业。。。如果现在是星期四上午8点,我想从现在开始获得1小时的NSDate。如果今天是星期四晚上7点,我希望下个星期四的日期是上午9点
我计划在iCal中制作一个事件(测试已经成功),但问题在于计算事件时间
你能给我指出一个关于NSDate的好解释,或者帮我弄清楚如何计算我要找的NSDate吗
我希望修复此代码:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Bakery's Open!";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
要查找这样的日期,需要使用NSDateComponents和NSCalendar。我认为主要的任务是计算出你的目标日期是多少天。为此,您需要获取当前日期的工作日日期组件,并计算您要查找的工作日的天数。我认为以下代码应该可以让您接近:
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
NSArray *array = [NSArray arrayWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
NSDate *eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
[eventComps release];
return eventDate;
}干得好,这很管用!我很快会把这个工作示例贴在上面。
-(NSDate *)getNextDateOn:(NSString *)weekday atHour:(NSInteger)hour minute:(NSInteger)mins second:(NSInteger)secs {
NSArray *array = [NSArray arrayWithObjects:@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday",nil];
NSInteger weekdayNumber = [array indexOfObject:[weekday capitalizedString]] + 1;
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *nowComps = [cal components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
NSInteger daysForward = (weekdayNumber - nowComps.weekday + 7) % 7;
NSDateComponents *eventComps = [[NSDateComponents alloc] init];
[eventComps setDay:daysForward];
[eventComps setHour: hour - nowComps.hour];
[eventComps setMinute: mins - nowComps.minute];
[eventComps setSecond: secs - nowComps.second];
NSDate *eventDate = [cal dateByAddingComponents:eventComps toDate:now options:0];
[eventComps release];
return eventDate;