新建一个NSString(Time)分类,创建类方法实现时间的转换
.h文件
#import <Foundation/Foundation.h> @interface NSString (KJTime) //时间戳--->时间 +(NSString *)transToTime:(NSString *)timsp; //时间戳--->日期 +(NSString *)transToDate:(NSString *)timsp; //时间---->时间戳 +(NSString *)transTotimeSp:(NSString *)time; @end
.m文件
#import "NSString+KJTime.h" @implementation NSString (KJTime) //时间戳--->时间 +(NSString *)transToTime:(NSString *)timsp{ NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区 //设定时间格式,这里可以设置成自己需要的格式 [dateFormatter setDateFormat:@"MM/dd HH:mm"]; NSString *currentDateStr = [dateFormatter stringFromDate: detaildate]; return currentDateStr; } //时间戳--->日期 +(NSString *)transToDate:(NSString *)timsp{ NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区 //设定时间格式,这里可以设置成自己需要的格式 [dateFormatter setDateFormat:@"MM月dd日"]; NSString *currentDateStr = [dateFormatter stringFromDate: detaildate]; return currentDateStr; } //时间---->时间戳 +(NSString *)transTotimeSp:(NSString *)time{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //设置本地时区 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate *date = [dateFormatter dateFromString:time]; NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];//时间戳 return timeSp; } @end
时间: 2024-12-29 11:15:23