時間戳與時間互轉(zhuǎn)函數(shù)
swift版
//MARK: -時間戳轉(zhuǎn)時間函數(shù)
func timeStampToString(timeStamp: Double, outputFormatter: String)->String {
//時間戳為毫秒級要/1000 (13位數(shù))霜定, 秒就不用除1000(10位數(shù)),參數(shù)帶沒帶000
let timeString = String.init(format: "%d", timeStamp)
let timeSta:TimeInterval
if timeString.count == 10 {
timeSta = TimeInterval(timeStamp)
}else{
timeSta = TimeInterval(timeStamp / 1000)
}
let date = NSDate(timeIntervalSince1970: timeSta)
let dfmatter = DateFormatter()
//設(shè)定時間格式,這里可以設(shè)置成自己需要的格式y(tǒng)yyy-MM-dd HH:mm:ss
dfmatter.dateFormat = outputFormatter
return dfmatter.string(from: date as Date)
}
//MARK: -時間轉(zhuǎn)時間戳函數(shù)
func timeToTimeStamp(time: String ,inputFormatter:String) -> Double {
let dfmatter = DateFormatter()
//設(shè)定時間格式,這里可以設(shè)置成自己需要的格式
dfmatter.dateFormat = inputFormatter
let last = dfmatter.date(from: time)
let timeStamp = last?.timeIntervalSince1970
return timeStamp!
}
OC 版
/**
時間戳轉(zhuǎn)時間
@param timeStamp 時間戳
@return 時間字符串
*/
+ (NSString *)timeStampTransToTime:(NSString *)timeStamp outputFormatter:(NSString *)outputFormatter
{
NSTimeInterval time;
if (timeStamp.length == 10) {//如果后臺返回10位不除以1000,返回13位需要除以1000
time=[timeStamp doubleValue];
}else{
time=[timeStamp doubleValue]/1000;
}
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//設(shè)置本地時區(qū)
//設(shè)定時間格式,這里可以設(shè)置成自己需要的格式
[dateFormatter setDateFormat:outputFormatter];
NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
return currentDateStr;
}
/**
時間轉(zhuǎn)時間戳
@param time 時間
@return 時間戳
*/
+(NSString *)timeTransTotimeStamp:(NSString *)time inputFormatter:(NSString *)inputFormatter
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //設(shè)置本地時區(qū)
[dateFormatter setDateFormat:inputFormatter];
NSDate *date = [dateFormatter dateFromString:time];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];//時間戳
return timeSp;
}
根據(jù)時間戳轉(zhuǎn)化為剛剛暮顺,幾分鐘前
//OC
+(NSString *)handleDate:(NSString *)time_shijianchou{
NSMutableString *str;
NSDate *date=[NSDate date];
NSTimeInterval st=date.timeIntervalSince1970;
// NSDate *newDate=[NSDate dateWithTimeIntervalSince1970:st];
NSTimeInterval result=st-[time_shijianchou doubleValue];
if (result<60) {
str=[NSMutableString stringWithFormat:@"剛剛"];
}
else if (result>=60&&result<3600) {
str=[NSMutableString stringWithFormat:@"%d分鐘前",(int)result/60];
}
else if (result>=3600&&result<60*60*24) {
str=[NSMutableString stringWithFormat:@"%d小時前",(int)result/(60*60)];
}
else if (result>=60*60*24&&result<60*60*24*30) {
str=[NSMutableString stringWithFormat:@"%d天前",(int)result/(60*60*24)];
}
else if(result>=60*60*24*30&&result<60*60*24*30*12){
str=[NSMutableString stringWithFormat:@"%d個月",(int)result/(60*60*24*30)];
}else{
NSDate *resultDate=[NSDate dateWithTimeIntervalSince1970:st];
NSDateFormatter *df=[NSDateFormatter new];
[df setDateFormat:@"YYY年"];
str=[NSMutableString stringWithFormat:@"%@",[df stringFromDate:resultDate]];
}
return str;
}
// swift
//MARK: -根據(jù)后臺時間戳返回幾分鐘前乖篷,幾小時前响驴,幾天前
func updateTimeToCurrennTime(timeStamp: Double) -> String {
//獲取當前的時間戳
let currentTime = Date().timeIntervalSince1970
//時間戳為毫秒級要 / 1000, 秒就不用除1000撕蔼,參數(shù)帶沒帶000
let timeSta:TimeInterval = TimeInterval(timeStamp / 1000)
//時間差
let reduceTime : TimeInterval = currentTime - timeSta
//時間差小于60秒
if reduceTime < 60 {
return "剛剛"
}
//時間差大于一分鐘小于60分鐘內(nèi)
let mins = Int(reduceTime / 60)
if mins < 60 {
return "\(mins)分鐘前"
}
let hours = Int(reduceTime / 3600)
if hours < 24 {
return "\(hours)小時前"
}
let days = Int(reduceTime / 3600 / 24)
if days < 30 {
return "\(days)天前"
}
//不滿足上述條件---或者是未來日期-----直接返回日期
let date = NSDate(timeIntervalSince1970: timeSta)
let dfmatter = DateFormatter()
//yyyy-MM-dd HH:mm:ss
dfmatter.dateFormat="yyyy年MM月dd日 HH:mm:ss"
return dfmatter.string(from: date as Date)
}