#pragma mark - 將某個時間轉化成 時間戳
+(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轉成nsdate
? ? //時間轉時間戳的方法:
? ? NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
? ? NSLog(@"將某個時間轉化成 時間戳&&&&&&&timeSp:%ld",(long)timeSp); //時間戳的值
? ? return timeSp;
}
//時間戳轉成時間
//字符串轉為時間,時間格式自己定
+(NSString *)timestampSwitchTime:(NSString *)time{
//? ? NSString * time = @"594947489";//時間字符串
? ? NSInteger num = [time integerValue];//轉為int型
? ? NSDateFormatter * formatter = [[NSDateFormatter alloc] init];//時間管理
? ? [formatter setDateStyle:NSDateFormatterMediumStyle];//時間方式
? ? [formatter setTimeStyle:NSDateFormatterShortStyle];
? ? [formatter setDateFormat:@"YYYY-MM-dd"];
? ? NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
? ? [formatter setTimeZone:timeZone];
? ? NSDate * confromTime = [NSDate dateWithTimeIntervalSince1970:num];
? ? NSString * comfromTimeStr = [formatter stringFromDate:confromTime];
? ? NSLog(@"%@",comfromTimeStr);
? ? return comfromTimeStr;
}
//GMT只需要將代碼中的UTC替換為GMT即可
//將本地日期字符串轉為UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定輸入輸出格式
+ (NSString *)getUTCFormateLocalDate:(NSString *)localDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
? ? //輸入格式
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? ? NSDate *dateFormatted = [dateFormatter dateFromString:localDate];NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone];
? ? //輸出格式
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
? ? NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
? ? return dateString;
}
//將UTC日期字符串轉為本地時間字符串
//輸入的UTC日期格式2013-08-03T04:53:51+0000
+ (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
? ? //輸入格式
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; [dateFormatter setTimeZone:localTimeZone];
? ? NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
? ? //輸出格式
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? ? NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
? ? return dateString;
}
//將GMT日期字符串轉為本地時間字符串
//輸入的GMT日期格式Tue May 31 17:46:55 +0800 2011
+ (NSString *)getLocalDateFormateGMTDate:(NSString *)gmtDate{
? ? // 時間字符串
//? ? NSString *string = @"Tue May 31 17:46:55 +0800 2011";
? ? // 日期格式化類
? ? NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
? ? dateFormatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
? ? //fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy";
? ? // 設置語言區(qū)域
? ? NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; [dateFormatter setTimeZone:localTimeZone];
? ? NSDate *dateFormatted = [dateFormatter dateFromString:gmtDate];
? ? //輸出格式
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? ? NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
? ? return dateString;
}
//方式二 后臺給的格式為 純數(shù)字1352170595000(13位)
+(NSString *)updateTimeForRow:(NSString *)str {
? ? // 獲取當前時時間戳 1466386762.345715 十位整數(shù) 6位小數(shù)
? ? NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
? ? // 創(chuàng)建歌曲時間戳(后臺返回的時間 一般是13位數(shù)字)
? ? NSTimeInterval createTime =[str floatValue]/1000;
? ? // 時間差
? ? NSTimeInterval time = currentTime - createTime;
? ? //秒轉分鐘
? ? NSInteger small = time / 60;
? ? if (small == 0) {
? ? ? ? return [NSString stringWithFormat:@"剛剛"];
? ? }
? ? if (small < 60) {
? ? ? ? return [NSString stringWithFormat:@"%ld分鐘前",small];
? ? }
? ? // 秒轉小時
? ? NSInteger hours = time/3600;
? ? if (hours<24) {
? ? ? ? return [NSString stringWithFormat:@"%ld小時前",hours];
? ? }
? ? //秒轉天數(shù)
? ? NSInteger days = time/3600/24;
? ? if (days < 30) {
? ? ? ? return [NSString stringWithFormat:@"%ld天前",days];
? ? }
? ? //秒轉月
? ? NSInteger months = time/3600/24/30;
? ? if (months < 12) {
? ? ? ? return [NSString stringWithFormat:@"%ld月前",months];
? ? }
? ? //秒轉年
? ? NSInteger years = time/3600/24/30/12;
? ? return [NSString stringWithFormat:@"%ld年前",years];
}
//方式一 后臺給的格式為yyyy-MM-dd HH:mm:ss.SSS
+(NSString *) compareCurrentTime:(NSString *)str
{
? ? //把字符串轉為NSdate
? ? NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
? ? [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? ? NSDate *timeDate = [dateFormatter dateFromString:str];
? ? NSDate *currentDate = [NSDate date];
? ? //得到兩個時間差
? ? NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
? ? long temp = 0;
? ? NSString *result;
? ? if (timeInterval/60 < 1)
? ? {
? ? ? ? result = [NSString stringWithFormat:@"剛剛"];
? ? }
? ? else if((temp = timeInterval/60) <60){
? ? ? ? result = [NSString stringWithFormat:@"%ld分鐘前",temp];
? ? }
? ? else if((temp = temp/60) <24){
? ? ? ? result = [NSString stringWithFormat:@"%ld小時前",temp];
? ? }
? ? else if((temp = temp/24) <30){
? ? ? ? result = [NSString stringWithFormat:@"%ld天前",temp];
? ? }
? ? else if((temp = temp/30) <12){
? ? ? ? result = [NSString stringWithFormat:@"%ld月前",temp];
? ? } else{
? ? ? ? temp = temp/12;
? ? ? ? result = [NSString stringWithFormat:@"%ld年前",temp];
? ? }
? ? return? result;
}
原文鏈接:https://blog.csdn.net/dkq972958298/article/details/77931453