-
什么是UTC時間
- 協調世界時(英:Coordinated Universal Time ,法:Temps Universel Coordonné),又稱世界統一時間,世界標準時間,國際協調時間。英文(CUT)和法文(TUC)的縮寫不同,作為妥協,簡稱UTC(摘自百度百科) 。
- 中國大陸、中國香港、中國澳門、中國臺灣、蒙古國、新加坡、馬來西亞、菲律賓、西澳大利亞州的時間與UTC的時差均為+8,也就是UTC+8(相差八個小時)
- 這套時間系統被應用于許多互聯網和萬維網的標準中,因此在日常開發中UTC時間的使用較為常見
-
格林尼治標準時(GMT)
- 是指位于倫敦郊區的皇家格林尼治天文臺的標準時間(開發中不常用)
-
特別注意:
- iOS中的NSDate對象存放的日期始終是UTC的標準時間(比如下面的例子,服務器返回的字符串是utc時間,本地時區是北京)
- 有結果可知:時間字符串轉成NSDate時,沒有指定時間字符串的時區,系統會根據本地時區,將時間字符串轉成utc時間存放在NSDate對象中(通過Summary可以看出),而NSLog打印NSDate時,又會根據當地時區將utc時間轉成本地時區時間打印出來。
- 結論:NSDate中存放的時間會自動轉換成utc時間,NSLog打印的時間會自動根據時區打印不同的結果
NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *timeStr = @"2017-10-25 02:07:39"; //將時間字符串默認當本地時區處理,轉成NSDate時 -8,打印時再 +8 NSDate *timeDate = [format dateFromString:timeStr]; // Summary 2017-10-24 18:07:39 UTC NSLog(@"timeDate = %@", timeDate); //timeDate = Wed Oct 25 02:07:39 2017 format.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; //將時間字符串當utc處理,打印時根據本地時區自動打印 +8 NSDate *utcDate = [format dateFromString:timeStr]; // Summary 2017-10-25 02:07:39 UTC NSLog(@"timeDate = %@", utcDate); //timeDate = Wed Oct 25 10:07:39 2017
轉換函數
/**
anyDate 轉成 本地時區的 NSDate
*/
- (NSDate *)getLocalDateFormatAnyDate:(NSDate *)anyDate {
NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
NSTimeZone *desTimeZone = [NSTimeZone localTimeZone];
//得到源日期與世界標準時間的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目標日期與本地時區的偏移量
NSInteger destinationGMTOffset = [desTimeZone secondsFromGMTForDate:anyDate];
//得到時間偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//轉為現在時間
NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
return destinationDateNow;
}
/**
將本地日期字符串轉為UTC日期字符串
eg: 2017-10-25 02:07:39 -> 2017-10-24 18:07:39
*/
- (NSString *)getUTCStrFormateLocalStr:(NSString *)localStr {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *dateFormatted = [format dateFromString:localStr];
format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSString *dateString = [format stringFromDate:dateFormatted];
return dateString;
}
/**
將UTC日期字符串轉為本地時間字符串
eg: 2017-10-25 02:07:39 -> 2017-10-25 10:07:39
*/
- (NSString *)getLocalDateFormateUTCDate:(NSString *)utcStr {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *utcDate = [format dateFromString:utcStr];
format.timeZone = [NSTimeZone localTimeZone];
NSString *dateString = [format stringFromDate:utcDate];
return dateString;
}
- 演示結果:
NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd HH:mm:ss"; format.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSString *utcStr = @"2017-10-25 02:07:39"; NSDate *utcDate = [format dateFromString:utcStr]; // Summary 2017-10-25 02:07:39 UTC NSDate *localDate = [self getLocalDateFormatAnyDate:utcDate]; // Summary 2017-10-25 10:07:39 UTC NSLog(@" utcDate = %@", [NSString stringWithFormat:@"%@", utcDate]); //utcDate = 2017-10-25 02:07:39 +0000 NSLog(@"localDate = %@", [NSString stringWithFormat:@"%@", localDate]); //localDate = 2017-10-25 10:07:39 +0000 //這樣打印的話,NSLog又會給localDate +8 NSLog(@"localDate = %@", localDate); //localDate = Wed Oct 25 18:07:39 2017