</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
iOS时间类型转换和各种数据类型进行转换
最新推荐文章于 2019-12-19 11:25:57 发布
本文详细介绍了在iOS开发中如何进行时间类型转换,包括从1970年开始的秒数转换、时间戳与NSDate之间的转换、时区调整以及时间差计算。此外,还涵盖了日期格式化和时间戳的相互转换,以及获取当前系统时间、年月日格式转换等实用方法。同时,文章还涉及了数据类型转换,如16进制与NSData、NSString之间的转换,以及汉字与UTF8、GBK编码的互换操作。
摘要由CSDN通过智能技术生成