给你写个NSString方法(可以写在分类,也可以写在控制器直接调用),调用下面方法设置传入参数hour为2即可,其他参数为零。 注意看你本地是否处理了格林尼时区时间,如果处理了可以不用加8,看打印吧 /** * ** 在当前日期时间加上 某个时间段(传负数即返回当前时间之前x月x日的时间) * * @param year 当前时间若干年后 (传负数为当前时间若干年前) * @param month 当前时间若干月后 (传0即与当前时间一样) * @param day 当前时间若干天后 * @param hour 当前时间若干小时后 * @param minute 当前时间若干分钟后 * @param second 当前时间若干秒后 * * @return 处理后的时间字符串 */ - (NSString *)dateStringAfterlocalDateForYear:(NSInteger)year Month:(NSInteger)month Day:(NSInteger)day Hour:(NSInteger)hour Minute:(NSInteger)minute Second:(NSInteger)second { // 当前日期 NSDate *localDate = [NSDate date]; // 为伦敦时间 // 在当前日期时间加上 时间:格里高利历 NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *offsetComponent = [[NSDateComponents alloc]init]; [offsetComponent setYear:year ]; // 设置开始时间为当前时间的前x年 [offsetComponent setMonth:month]; [offsetComponent setDay:day]; [offsetComponent setHour:(hour+8)]; // 中国时区为正八区,未处理为本地,所以+8 [offsetComponent setMinute:minute]; [offsetComponent setSecond:second]; // 当前时间后若干时间 NSDate *minDate = [gregorian dateByAddingComponents:offsetComponent toDate:localDate options:0]; NSString *dateString = [NSString stringWithFormat:@"%@",minDate]; return dateString; } |
|