ios 时间 string 和 nsdate问题 swift2 2 oc

///根据传入的时间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; }

转载于:https://juejin.im/post/5a31f12e6fb9a0450310199a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值