iOS獲取時(shí)間差 獲取時(shí)間 是否是24小時(shí)制

直接上代碼

// 本地時(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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容