直接上代碼
// 本地時(shí)間與其他時(shí)間對(duì)比,判斷是否超過(guò)固定時(shí)間
+ (BOOL)overtime:(NSString *)serverTime maxSecond:(CGFloat)maxSec
{
if (serverTime.length == 0 || maxSec < 0) {
return NO;
}
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// fmt.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
// fmt.locale =[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
if ([self is12Or24Time]) {
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
}else{
fmt.dateFormat = @"yyyy-MM-dd hh:mm:ss";
}
NSDate *serverDate = [fmt dateFromString:serverTime];
// NSInteger min = [[NSDate date] minutesAfterDate:serverDate];
if (serverDate == nil) {
return NO;
}
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:serverDate];
NSLog(@"------%.f-------",timeDiff);
if (fabs(timeDiff) > maxSec) {
return YES;
}
return NO;
}
//當(dāng)前時(shí)間 str類(lèi)型
+ (NSString *)getCurrentTimeString
{
NSString *sss = @"yyyy-MM-dd hh:mm:ss";
if ([self is12Or24Time]) {
sss = @"yyyy-MM-dd HH:mm:ss";
}
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:sss];
NSString *dateTime=[formatter stringFromDate:[NSDate date]];
return dateTime;
}
+ (BOOL)is12Or24Time
{
//獲取系統(tǒng)是24小時(shí)制或者12小時(shí)制
NSString*formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
BOOL hasAMPM = containsA.location != NSNotFound;
//hasAMPM==TURE為12小時(shí)制,否則為24小時(shí)制
return hasAMPM;
}