//將UTCDate(世界標(biāo)準(zhǔn)時(shí)間)轉(zhuǎn)化為當(dāng)?shù)貢r(shí)區(qū)的標(biāo)準(zhǔn)Date(鐘表顯示的時(shí)間)
//NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000?
//轉(zhuǎn)化后:2018-03-27 14:54:41 +0000?
-(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{
NSTimeZone *tz = [NSTimeZone defaultTimeZone]; 7NSInteger seconds = [tz secondsFromGMTForDate: UTCDate];?
?return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate];?
}?
//將當(dāng)?shù)貢r(shí)區(qū)的標(biāo)準(zhǔn)Date轉(zhuǎn)化為UTCDate?
//當(dāng)前當(dāng)?shù)氐臉?biāo)準(zhǔn)時(shí)間:2018-03-27 14:54:41 +0000?
//轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間: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];?
}?
//根據(jù)UTCDate獲取當(dāng)前時(shí)間字符串(鐘表上顯示的時(shí)間)?
//輸入:[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;?
}?
//根據(jù)UTC字符串獲取當(dāng)前時(shí)間字符串(鐘表上顯示的時(shí)間)?
//輸入:2018-03-27 07:44:05?
//輸出:2018-03-27 15:44:05?
-(NSString *)localStringFromUTCString:(NSString *)UTCString{?
//先將UTC字符串轉(zhuǎn)為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;?
?}?
//將當(dāng)前時(shí)間字符串轉(zhuǎn)為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;
}
//將當(dāng)前時(shí)間字符串轉(zhuǎn)為UTC字符串?
?-(NSString *)UTCStringFromLocalString:(NSString *)localString{
NSDate *date = [self UTCDateFromLocalString:localString];?
NSString *string= [NSString stringWithFormat:@"%@",date];?
NSString *result = [stringsubstringToIndex:string.length-6];
?return result;?
?}?
//UTCDate轉(zhuǎn)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;?
}?
//將當(dāng)前時(shí)間(UTCDate)轉(zhuǎn)為時(shí)間戳?
-(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{?
NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970];?
// *1000,是精確到毫秒蔫敲;這里是精確到秒;
?NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];
return result;?
}?
//當(dāng)前時(shí)間字符串(鐘表上顯示的時(shí)間)轉(zhuǎn)為時(shí)間戳?
-(NSString *)timeStamapFromLocalString:(NSString *)localString{
//先轉(zhuǎn)為UTCDate?
NSDate *UTCDate = [self UTCDateFromLocalString:localString];
NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
return timeStamap;
}
//將UTCString轉(zhuǎn)為時(shí)間戳
-(NSString *)timeStamapFromUTCString:(NSString *)UTCString{
//先將UTC字符串轉(zhuǎn)為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;
}
//時(shí)間戳轉(zhuǎn)UTCDate
-(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{
NSTimeInterval timeInterval=[timeStamap doubleValue];
// ? 1000; 傳入的時(shí)間戳timeStamap如果是精確到毫秒的記得要/1000
NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];
return UTCDate;
}