</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时间类型转换和各种数据类型进行转换
最新推荐文章于 2021-01-07 20:44:08 发布