1. 当前时间
1 NSDate * now = [NSDate date];
2. 根据已有时间创建新时间
1 NSDate * tomorrow = [NSDate dateWithTimeInterval:60*60*24 sinceDate:now]; 2 NSDate * date1 = [NSDate dateWithTimeIntervalSince1970:0];
3.1 使用NSDateComponents
1 NSDateComponents * comp = [[NSDateComponents alloc] init]; 2 comp.year = 2014; 3 comp.month = 4; 4 comp.day = 24; 5 comp.hour = 7; 6 comp.minute = 30; 7 comp.second = 30; 8 // 注意,这里是格林尼治,也可以选择中国,日本,美国 9 NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 10 NSDate *myDate = [myCal dateFromComponents:comp]; 11 NSLog(@"%myDate = %@", myDate); 12 newNotification.fireDate = myDate;
3.2 使用NSDateFormatter
1 NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 2 formatter.timeZone = [NSTimeZone defaultTimeZone]; 3 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 4 NSDate *myDate2 = [formatter dateFromString:@"2014-04-24 7:30:30"];