使用网络时间进行租赁周期选择、送还货时间选择,避免当前时间和服务器时间不同步/一些用户恶搞,修改本地时间,造成无效订单,业务看起来不复杂,但处理起来还是稍微有点麻烦:
1.除了时间真实性和及时性的问题,还要注意上方加减周期和下方换货时间的联动问题,送货时间和换货时间切换后再改变上方加减周期的联动问题;
2.下面时间的获取、判定、比较代码只是其中的一部分,更多内容可以看我的Demo;
一起来看一下效果:
项目实战
技术点:
- 获取网络时间/服务器时间
//获取网络时间
+ (void)getNetWorkDateSuccess:(void(^)(NSDate *networkDate, NSString *networkDateStr))success {
NSString *urlString = @"https://www.baidu.com";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:5];
[request setHTTPShouldHandleCookies:FALSE];
[request setHTTPMethod:@"GET"];
__block NSDate *localDate;
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (!connectionError) {
//要把网络数据强转 不然用不了下面那个方法获取不到内容(个人感觉,不知道对不)
NSHTTPURLResponse *responsee = (NSHTTPURLResponse *)response;
NSString *date = [[responsee allHeaderFields] objectForKey:@"Date"];
date = [date substringFromIndex:5];
date = [date substringToIndex:[date length]-4];
NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];
dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
//处理八小时问题-这里获取到的是标准时间
NSDate *netDate = [dMatter dateFromString:date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:netDate];
NSLog(@"interval: %ld", interval);
localDate = [netDate dateByAddingTimeInterval: interval];
NSLog(@"%@________1",localDate);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *nowtimeStr = [NSString string];
nowtimeStr = [formatter stringFromDate:localDate];
NSLog(@"%@--------2",nowtimeStr);
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
success(localDate, nowtimeStr);
}
});
}else {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showError:@"网络异常, 请稍后重试"];
});
}
}];
}
- 时间计算-增/减日期
//根据时间间隔计算新的时间 年-月-日 字符串格式(年-月-日)
+ (NSString *)updateDateWithDateStr:(NSString *)dateStr Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd {
NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSTimeInterval fromTime = [[formatter dateFromString:dateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
}else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
}
//根据时间间隔计算新的时间 年-月-日
+ (NSString *)updateDateWithDate:(NSDate *)date Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd {
NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *fromDateStr = [formatter stringFromDate:date];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
}else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
}
//根据时间间隔计算新的时间 年-月-日 时:分:秒
+ (NSString *)updatePreciseDateWithDate:(NSDate *)date Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd {
NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *fromDateStr = [formatter stringFromDate:date];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
}else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
}
3.时间间隔判断
#pragma mark - 判断当前时间是否在某一时间段
/**
@param fromDate 开始时间 @“10:00”
@param toDate 结束时间 @“15:00”
@return 判断结果
*/
- (BOOL)isOpenTimeWithFromDate:(NSString *)fromDate toDate:(NSString *)toDate {
//获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr = [formatter stringFromDate:_networkDate];
//开始时间和结束时间
NSString *fromDateStr = [NSString stringWithFormat:@"%@ %@", dateStr, fromDate];
NSString *endDateStr = [NSString stringWithFormat:@"%@ %@", dateStr, toDate];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
NSTimeInterval endTime = [[formatter dateFromString:endDateStr] timeIntervalSince1970];
NSTimeInterval currentTime = [_networkDate timeIntervalSince1970];
if (currentTime > fromTime && currentTime < endTime) {
return YES;
}else {
return NO;
}
}
#pragma mark - 判断当前时间是否处于某一时间之前
/**
判断当前时间是否处于某一时间之前
@param currentDate 当前时间
@param toDate 比较时间
@return 比较结果
*/
- (BOOL)isGreaterThanWithCurrentDate:(NSDate *)currentDate ToDate:(NSString *)toDate {
//获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr = [formatter stringFromDate:currentDate];
//开始时间和结束时间
NSString *toDateStr = [NSString stringWithFormat:@"%@ %@", dateStr, toDate];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSTimeInterval toTime = [[formatter dateFromString:toDateStr] timeIntervalSince1970];
NSTimeInterval currentTime = [currentDate timeIntervalSince1970];
if (currentTime > toTime) {
return YES;
}else {
return NO;
}
}
网友评论