將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間
NSString *res = [self timestampSwitchTime:[timestamp integerValue] andFormatter:@"YYYY-MM-dd hh:mm:ss"];
#pragma mark - 將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間
+ (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
/// 在當(dāng)前時(shí)間,后退30分鐘 = 1800
// confromTimesp = [confromTimesp dateByAddingTimeInterval:-1800];
NSLog(@"1296035591 = %@",confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
return confromTimespStr;
}
計(jì)算兩個(gè)時(shí)間點(diǎn)差值
NSInteger vipLastTime = [self pleaseInsertStarTimeo:res andInsertEndTime:[PublicDatas session].userInfo.expireTime];
+ (NSInteger)pleaseInsertStarTimeo:(NSString *)time1 andInsertEndTime:(NSString *)time2{
// 1.將時(shí)間轉(zhuǎn)換為date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"YYYY-MM-dd hh:mm:ss";
NSDate *date1 = [formatter dateFromString:time1];
NSDate *date2 = [formatter dateFromString:time2];
// 2.創(chuàng)建日歷
NSCalendar *calendar = [NSCalendar currentCalendar];
// NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendarUnit type = NSCalendarUnitSecond;
// 3.利用日歷對(duì)象比較兩個(gè)時(shí)間的差值
NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0];
// 4.輸出結(jié)果
NSLog(@"兩個(gè)時(shí)間相差%ld年%ld月%ld日%ld小時(shí)%ld分鐘%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
return cmps.second;
}
?著作權(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ù)。