【iOS】常見NSDate與時間戳轉(zhuǎn)換

//時間戳轉(zhuǎn)時間 年月日
+ (NSString *)timeStringWithTimeInterval:(NSInteger)timeInterval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval]; //此處根據(jù)項目需求,選擇是否除以1000 , 如果時間戳精確到秒則去掉1000
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //    NSDate *currentDate = [NSDate date];//獲取當前時間,日
    //    NSDate *pastHalfHour = [currentDate dateByAddingTimeInterval:-3600]; // 半小時前是-1800   1小時后是3600   1小時前是-3600
    //
    //    NSInteger timeSp = [[NSNumber numberWithDouble:[pastHalfHour timeIntervalSince1970]] integerValue];
    //    if (timeInterval < timeSp) {
    //
    //    }
    formatter.dateFormat = @"YYYY-MM-dd  HH:mm:ss";
    return [formatter stringFromDate:date];
    //今天
    //    if ([date isToday]) {
    //
    //        formatter.dateFormat = @"HH:mm";
    //
    //        return [formatter stringFromDate:date];
    //    }else{
    //
    //        NSLog(@"%@", [formatter stringFromDate:date]);
    //        //昨天
    //        if ([date isYesterday]) {
    //
    //            formatter.dateFormat = @"昨天 HH:mm";
    //            return [formatter stringFromDate:date];
    //
    //            //一周內(nèi) [date weekdayStringFromDate]
    //        }else if ([date isSameWeek]){
    //
    //            formatter.dateFormat = [NSString stringWithFormat:@"%@ %@",[date weekdayStringFromDate],@"HH:mm"];
    //            return [formatter stringFromDate:date];
    //
    //            //直接顯示年月日
    //        }else{
    //
    //            formatter.dateFormat = @"yy-MM-dd  HH:mm";
    //            return [formatter stringFromDate:date];
    //        }
    //    }
    //    return nil;
}

//時間戳轉(zhuǎn)時間 年月日
+ (NSString *)timeWithTimestamp:(NSInteger )timestamp format:(NSString *)format {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp]; //此處根據(jù)項目需求,選擇是否除以1000 , 如果時間戳精確到秒則去掉1000
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------設置你想要的格式,hh與HH的區(qū)別:分別表示12小時制,24小時制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------將字符串按formatter轉(zhuǎn)成nsdate
    //時間轉(zhuǎn)時間戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return timeSp;
    
}

// 當前時間
+ (NSString *)getCurrentTimeyyyymmdd {
    
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    return dayStr;
}

/// 計算時間戳之間的時間差
+ (NSDateComponents *)getDateDifferenceWithBeginTimestamp:(NSString*)beginTimestamp
                                    endTimestamp:(NSString*)endTimestamp {
    
    NSTimeInterval timer1 = [beginTimestamp doubleValue];
    NSTimeInterval timer2 = [endTimestamp doubleValue];
    
    if (timer1 == 0 || timer2 ==0) {
        return 0;
    }
    
    if (beginTimestamp.length >= 13) {
        timer1 = timer1 / 1000;
    }
    if (endTimestamp.length >= 13) {
        timer2 = timer2 / 1000;
    }
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timer1];
    
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timer2];
        
    // 日歷對象(方便比較兩個日期之間的差距)
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:date2 options:0];
    
//    NSString *dateString1 = [formatter stringFromDate:date];
//    NSString *dateString2 = [formatter stringFromDate:date2];
//    NSLog(@"%@",dateString1);
//    NSLog(@"%@",dateString2);
//    // 獲得某個時間的年月日時分秒
//    NSLog(@"差值%ld月,%ld天,%ld小時%ld分%ld秒", cmps.month,cmps.day ,cmps.hour, cmps.minute,cmps.second);
        
    return cmps;
}
輸出結(jié)果:
2021-01-15 10:25:02
2021-08-19 20:56:17
差值7月,4天,10小時31分15秒

// 倒計時:天時分秒
+ (NSString *)downTimeWithTimeInterval:(NSInteger)timeInterval {
    
    NSInteger days = (int)(timeInterval/(3600*24));
    NSInteger hours = (int)((timeInterval-days*24*3600)/3600);
    NSInteger minute = (int)(timeInterval-days*24*3600-hours*3600)/60;
    NSInteger second = timeInterval-days*24*3600-hours*3600-minute*60;
    
    NSString *downTimeString = @"";
    if (days == 0) {
        downTimeString = @"";
    }
    if (days > 0) {
        downTimeString = [NSString stringWithFormat:@"%ld天%ld時%ld分%ld秒", days, hours, minute, second];
    } else {
        if (hours > 0) {
            if (hours < 10) {
                downTimeString = [NSString stringWithFormat:@"%0ld時%ld分%ld秒", hours, minute, second];
            } else {
                downTimeString = [NSString stringWithFormat:@"%ld時%ld分%ld秒", hours, minute, second];
            }
        } else {
            if (minute > 0) {
                if (minute < 10) {
                    downTimeString = [NSString stringWithFormat:@"%0ld分%ld秒", minute, second];
                } else {
                    downTimeString = [NSString stringWithFormat:@"%ld分%ld秒", minute, second];
                }
            } else {
                if (second < 10) {
                    downTimeString = [NSString stringWithFormat:@"%0ld秒", second];
                } else if (second == 0) {
                    downTimeString = @"";
                } else {
                    downTimeString = [NSString stringWithFormat:@"%ld秒", second];
                }
            }
        }
    }
    return downTimeString;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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