iOS開發NSDate、NSString、時間戳之間的轉化

 

 

 

 

//將UTCDate(世界標準時間)轉化爲當地時區的標準Date(鐘錶顯示的時間)

//NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000 
//轉化後:2018-03-27 14:54:41 +0000 
-(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: UTCDate];  
return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate];
 }

 

//將當地時區的標準Date轉化爲UTCDate 
//當前當地的標準時間:2018-03-27 14:54:41 +0000 
//轉化爲世界標準時間:2018-03-27 06:54:41 +0000 
-(NSDate *)getUTCDateFromLocalDate:(NSDate *)LocalDate{
 NSTimeZone *tz = [NSTimeZone defaultTimeZone];
 NSInteger seconds = -[tz secondsFromGMTForDate: LocalDate];
 return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate];
 }

 

//根據UTCDate獲取當前時間字符串(鐘錶上顯示的時間)
 //輸入:[NSDate date]     2018-03-27 07:44:05 +0000
 //輸出:2018-03-27 15:44:05  
-(NSString *)localStringFromUTCDate:(NSDate *)UTCDate{
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
NSTimeZone *tz = [NSTimeZone defaultTimeZone];  
[dateFormatter setTimeZone:tz];
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString* result=[dateFormatter stringFromDate:UTCDate]; 
return result; 
}

 

//根據UTC字符串獲取當前時間字符串(鐘錶上顯示的時間)
 //輸入:2018-03-27 07:44:05
 //輸出:2018-03-27 15:44:05
 -(NSString *)localStringFromUTCString:(NSString *)UTCString{ 
//先將UTC字符串轉爲UTCDate; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
 [dateFormatter setTimeZone:tz]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate = [dateFormatter dateFromString:UTCString];  
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString* result = [dateFormatter stringFromDate:UTCDate];  
return result;  }

 

//將當前時間字符串轉爲UTCDate 
-(NSDate *)UTCDateFromLocalString:(NSString *)localString{
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:localString]; 
return date;
}

 

//將當前時間字符串轉爲UTC字符串 
 -(NSString *)UTCStringFromLocalString:(NSString *)localString{
NSDate *date = [self UTCDateFromLocalString:localString]; 
NSString *string= [NSString stringWithFormat:@"%@",date]; 
NSString *result = [stringsubstringToIndex:string.length-6]; 
return result;  
}

 

//UTCDate轉UTC字符串
-(NSString *)UTCStringFromUTCDate:(NSDate *)UTCDate{ 
NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init]; 
[dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dataFormatter setTimeZone:tz];
NSString *UTCString = [dataFormatter stringFromDate:UTCDate]; 
return UTCString;
 }

 

//將當前時間(UTCDate)轉爲時間戳
 -(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{
 NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970]; 
// *1000,是精確到毫秒;這裏是精確到秒; 
NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];
return result; 
}

 

 

//當前時間字符串(鐘錶上顯示的時間)轉爲時間戳 
-(NSString *)timeStamapFromLocalString:(NSString *)localString{
//先轉爲UTCDate 
NSDate *UTCDate = [self UTCDateFromLocalString:localString];
NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
return timeStamap;
}

 

 

//將UTCString轉爲時間戳
-(NSString *)timeStamapFromUTCString:(NSString *)UTCString{
//先將UTC字符串轉爲UTCDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate = [dateFormatter dateFromString:UTCString];
NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
return timeStamap;
}

 

//時間戳轉UTCDate
-(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{
NSTimeInterval timeInterval=[timeStamap doubleValue];
//   1000; 傳入的時間戳timeStamap若是是精確到毫秒的記得要/1000
NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];
return UTCDate;
}
相關文章
相關標籤/搜索