iOS開發(fā)NSDate、NSString、時間戳之間的轉(zhuǎn)化

//將UTCDate(世界標(biāo)準(zhǔn)時間)轉(zhuǎn)化為當(dāng)?shù)貢r區(qū)的標(biāo)準(zhǔn)Date(鐘表顯示的時間)

//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區(qū)的標(biāo)準(zhǔn)Date轉(zhuǎn)化為UTCDate?

//當(dāng)前當(dāng)?shù)氐臉?biāo)準(zhǔn)時間:2018-03-27 14:54:41 +0000?

//轉(zhuǎn)化為世界標(biāo)準(zhǔn)時間: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)前時間字符串(鐘表上顯示的時間)?

//輸入:[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)前時間字符串(鐘表上顯示的時間)?

//輸入: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)前時間字符串轉(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)前時間字符串轉(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)前時間(UTCDate)轉(zhuǎn)為時間戳?

-(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{?

NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970];?

// *1000,是精確到毫秒;這里是精確到秒;

?NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];

return result;?

}?


//當(dāng)前時間字符串(鐘表上顯示的時間)轉(zhuǎn)為時間戳?

-(NSString *)timeStamapFromLocalString:(NSString *)localString{

//先轉(zhuǎn)為UTCDate?

NSDate *UTCDate = [self UTCDateFromLocalString:localString];

NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];

return timeStamap;

}


//將UTCString轉(zhuǎn)為時間戳

-(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;

}


//時間戳轉(zhuǎn)UTCDate

-(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{

NSTimeInterval timeInterval=[timeStamap doubleValue];

// ? 1000; 傳入的時間戳timeStamap如果是精確到毫秒的記得要/1000

NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];

return UTCDate;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容