The following two methods are available:
1. Use the date component NSDateComponents
2. Use NSString to generate a date
// Create a Calendar Object
NSCalendar * calendar = [NSCalendarcurrentCalendar];
// Create date component
NSDateComponents * dc = [[NSDateComponentsalloc] init];
// Set the current time
[Dc setCalendar: calendar];
[Dc setYear: 2014];
[Dc setMonth: 4];
[Dc setDay: 9];
[Dc setHour: 21];
[Dc setMinute: 26];
[Dc setSecond: 0];
NSString * dateStr = @ "2014-04-0921: 38: 00 ";
NSDateFormatter * formatter = [[NSDateFormatteralloc] init];
Formatter. dateFormat = @ "yyyy-MM-ddHH: mm: ss ";
NSDate * schuduleNate = [dcdate];
NSDate * schuduleNate2 = [formatterdateFromString: dateStr];
NSDate * dateNow = [NSDatedate];
// Calculate the time difference
NSTimeInterval num = [schuduleNate2timeIntervalSinceDate: dateNow];
NSLog (@ "% f and then start executing the task", num );
// Task pool
Nsunloop * mainLoop = [nsunloopcurrentrunloop];
//
NSTimer * timer = [NSTimerscheduledTimerWithTimeInterval: num target: self selector: @ selector (scheduleTask) userInfo: nil repeats: NO];
NSTimer * timer2 = [NSTimerscheduledTimerWithTimeInterval: num + 3 target: self selector: @ selector (scheduleTask) userInfo: nil repeats: NO];
// Add to task pool
[MainLoop addTimer: timer forMode: NSDefaultRunLoopMode];
[MainLoop addTimer: timer2 forMode: NSDefaultRunLoopMode];
[MainLoop run];
Output Information
21:37:54. 289 WeChat [1810: 60b] 5.710526
21:38:00. 001 WeChat [1810: 60b] do some thing
21:38:03. 001 WeChat [1810: 60b] do some thing