iOS时间类型转换和各种数据类型进行转换

本文详细介绍了在iOS开发中如何进行时间类型转换,包括从1970年开始的秒数转换、时间戳与NSDate之间的转换、时区调整以及时间差计算。此外,还涵盖了日期格式化和时间戳的相互转换,以及获取当前系统时间、年月日格式转换等实用方法。同时,文章还涉及了数据类型转换,如16进制与NSData、NSString之间的转换,以及汉字与UTF8、GBK编码的互换操作。
摘要由CSDN通过智能技术生成
</pre><pre name="code" class="objc">/**
 *  时间转换部分
 *
 //从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;
 }
 
 //将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳
 -(long)changeTimeToTimeSp:(NSString *)timeStr
 {
 long time;
 NSDateFormatter *format=[[NSDateFormatter alloc] init];
 [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSDate *fromdate=[format dateFromString:timeStr];
 time= (long)[fromdate timeIntervalSince1970];
 NSLog(@"%ld",time);
 return time;
 }
 
 //获取当前系统的yyyy-MM-dd HH:mm:ss格式时间
 -(NSString *)getTime
 {
 NSDate *fromdate=[NSDate date];
 NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
 [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSString* string=[dateFormat stringFromDate:fromdate];
 return string;
 }
 
 //将当前时间转化为年月日格式
 -(N
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值