iOS开发NSDate、NSString、时间戳之间的转化
程序员文章站
2022-11-28 16:56:28
1 //将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间) 2 //NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000 3 //转化后:2018-03-27 14:54:41 +0000 4 -(NSDate *... ......
1 //将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间) 2 //NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000 3 //转化后:2018-03-27 14:54:41 +0000 4 -(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{ 5 6 NSTimeZone *tz = [NSTimeZone defaultTimeZone]; 7 NSInteger seconds = [tz secondsFromGMTForDate: UTCDate]; 8 return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate]; 9 10 } 11 12 //将当地时区的标准Date转化为UTCDate 13 //当前当地的标准时间:2018-03-27 14:54:41 +0000 14 //转化为世界标准时间:2018-03-27 06:54:41 +0000 15 -(NSDate *)getUTCDateFromLocalDate:(NSDate *)LocalDate{ 16 17 NSTimeZone *tz = [NSTimeZone defaultTimeZone]; 18 NSInteger seconds = -[tz secondsFromGMTForDate: LocalDate]; 19 return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate]; 20 21 } 22 23 //根据UTCDate获取当前时间字符串(钟表上显示的时间) 24 //输入:[NSDate date] 2018-03-27 07:44:05 +0000 25 //输出:2018-03-27 15:44:05 26 -(NSString *)localStringFromUTCDate:(NSDate *)UTCDate{ 27 28 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 29 NSTimeZone *tz = [NSTimeZone defaultTimeZone]; 30 [dateFormatter setTimeZone:tz]; 31 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 32 NSString* result=[dateFormatter stringFromDate:UTCDate]; 33 return result; 34 35 } 36 37 //根据UTC字符串获取当前时间字符串(钟表上显示的时间) 38 //输入:2018-03-27 07:44:05 39 //输出:2018-03-27 15:44:05 40 -(NSString *)localStringFromUTCString:(NSString *)UTCString{ 41 42 //先将UTC字符串转为UTCDate; 43 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 44 NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 45 [dateFormatter setTimeZone:tz]; 46 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 47 NSDate *UTCDate = [dateFormatter dateFromString:UTCString]; 48 49 [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]]; 50 NSString* result = [dateFormatter stringFromDate:UTCDate]; 51 return result; 52 } 53 54 //将当前时间字符串转为UTCDate 55 -(NSDate *)UTCDateFromLocalString:(NSString *)localString{ 56 57 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 58 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 59 NSDate *date = [dateFormatter dateFromString:localString]; 60 return date; 61 } 62 63 //将当前时间字符串转为UTC字符串 64 -(NSString *)UTCStringFromLocalString:(NSString *)localString{ 65 66 NSDate *date = [self UTCDateFromLocalString:localString]; 67 NSString *string = [NSString stringWithFormat:@"%@",date]; 68 NSString *result = [string substringToIndex:string.length-6]; 69 return result; 70 71 } 72 73 //UTCDate转UTC字符串 74 -(NSString *)UTCStringFromUTCDate:(NSDate *)UTCDate{ 75 76 NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init]; 77 [dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 78 NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 79 [dataFormatter setTimeZone:tz]; 80 NSString *UTCString = [dataFormatter stringFromDate:UTCDate]; 81 return UTCString; 82 83 } 84 85 //将当前时间(UTCDate)转为时间戳 86 -(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{ 87 88 NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970]; 89 // *1000,是精确到毫秒;这里是精确到秒; 90 NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval]; 91 return result; 92 93 } 94 95 //当前时间字符串(钟表上显示的时间)转为时间戳 96 -(NSString *)timeStamapFromLocalString:(NSString *)localString{ 97 98 //先转为UTCDate 99 NSDate *UTCDate = [self UTCDateFromLocalString:localString]; 100 NSString *timeStamap = [self timeStampFromUTCDate:UTCDate]; 101 return timeStamap; 102 103 } 104 105 //将UTCString转为时间戳 106 -(NSString *)timeStamapFromUTCString:(NSString *)UTCString{ 107 108 //先将UTC字符串转为UTCDate; 109 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 110 NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 111 [dateFormatter setTimeZone:tz]; 112 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 113 NSDate *UTCDate = [dateFormatter dateFromString:UTCString]; 114 115 NSString *timeStamap = [self timeStampFromUTCDate:UTCDate]; 116 return timeStamap; 117 } 118 119 //时间戳转UTCDate 120 -(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{ 121 122 NSTimeInterval timeInterval=[timeStamap doubleValue]; 123 // /1000;传入的时间戳timeStamap如果是精确到毫秒的记得要/1000 124 NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval]; 125 return UTCDate; 126 127 }
若是想要将时间戳转为字符串,可根据获得的UTCDate再进行转化