iOS開發NSDate、NSString、時間戳記之間的轉化,nsdatensstring
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 //時間戳記轉UTCDate120 -(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{121 122 NSTimeInterval timeInterval=[timeStamap doubleValue];123 // /1000;傳入的時間戳記timeStamap如果是精確到毫秒的記得要/1000124 NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];125 return UTCDate;126 127 }
若是想要將時間戳記轉為字串,可根據獲得的UTCDate再進行轉化