///根据传入的时间date,转换成字符串
class func timeDoubleTimeString(date:NSDate)->(String){
let localeDate = NSDate()
///日期转化成时间
let timeDouble:Double = fabs(localeDate.timeIntervalSinceDate(date))
let time = Int(timeDouble)
if time<60{return "\(time)秒前"}
if time<60*60{return "\(time/60)分钟前"}
if time<60*60*24{return "\(time/(60*60))小时前"}
///过1天 这里返回日期,
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "MM-dd HH:mm"//yyyy-MM-dd HH:mm:ss"
return dateFormat.stringFromDate(date)
}
复制代码
/**
- 时间转换部分
//从1970年开始到现在经过了多少秒 -(NSString *)getTimeSp { NSString *time; NSDate *fromdate=[NSDate date]; time = [NSString stringWithFormat:@"%f",[fromdate timeIntervalSince1970]]; return time; }
//将时间戳转换成NSDate,转换的时间我也不知道是哪国时间,应该是格林尼治时间 -(NSDate )changeSpToTime:(NSString)spString { NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]]; NSLog(@"%@",confromTimesp); return confromTimesp; }
//将时间戳转换成NSDate,加上时区偏移。这个转换之后是北京时间 -(NSDate*)zoneChange:(NSString*)spString { NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate:confromTimesp]; NSDate *localeDate = [confromTimesp dateByAddingTimeInterval: interval]; NSLog(@"%@",localeDate); return localeDate; }
//比较给定NSDate与当前时间的时间差,返回相差的秒数 -(long)timeDifference:(NSDate *)date { NSDate *localeDate = [NSDate date]; long difference =fabs([localeDate timeIntervalSinceDate:date]); return difference; }
//将NSDate按yyyy-MM-dd HH:mm:ss格式时间输出 -(NSString*)nsdateToString:(NSDate *)date { NSDateFormatter dateFormat=[[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString string=[dateFormat stringFromDate:date]; NSLog(@"%@",string); return string; }