1、在iOS上打印NSDate的本地时间时候,不管你怎么改变时间,打印出来的结果都是一样的,因为NSDate默认的description函数都是直接打印UTC时间。
2、不管是在世界上哪个时区,
- (NSTimeInterval)timeIntervalSince1970;
打印这个时间,都会是相同的间隔。
3、世界上所有的时间都是以这个时间为基准进行转换的,当进行时区转换的时候,基本思想应该是转换为UTC时间,然后再转换到目标时区的时间。如果有NSFormater就很强大了。
http://note.tc.edu.tw/640.html
4、时区转换的方法
//源时区的时间是东八区时间.这是正确的转换方法.
NSString *str =@"13-6-18:12:28";
NSString *dateFormatString =@"yy-MM-dd:HH:mm";
- (NSString *)convertDateToCurrLocal:(NSString *)string ofSpecifyFormate:(NSString *)dateFormatString
{
NSDateFormatter *inputFormatter = [[NSDateFormatteralloc]init];
[inputFormattersetDateFormat:dateFormatString];
[inputFormatter setTimeZone:[NSTimeZonetimeZoneWithName:@"Asia/Shanghai"]];//設置源時間時區
NSDate *formatterDate = [inputFormatterdateFromString:string];
NSDateFormatter *outputFormatter = [[NSDateFormatteralloc]init];
[outputFormatter setTimeZone:[NSTimeZonelocalTimeZone]];
[outputFormattersetDateFormat:dateFormatString];
NSString *result = [outputFormatterstringFromDate:formatterDate];
[inputFormatterrelease];
[outputFormatterrelease];
return result;
}
5、计算时区差得方法.
- (NSDate *)convertSourceDateToDestDate:(NSDate *)sourceDate
{
NSTimeZone *srcTimeZone = [NSTimeZonetimeZoneWithName:@"Asia/Shanghai"];
NSTimeZone *dstTimeZone = [NSTimeZonesystemTimeZone];
NSInteger srcGMTOffset = [srcTimeZonesecondsFromGMTForDate:sourceDate];
NSInteger dstGMTOffset = [dstTimeZonesecondsFromGMTForDate:sourceDate];
NSTimeInterval interval = dstGMTOffset - srcGMTOffset;
NSDate *dstDate = [[[NSDatealloc]initWithTimeInterval:intervalsinceDate:sourceDate]autorelease];
return dstDate;
}
6、defaultTimeZone localTimeZone systemTimeZone区别;
连接: http://stackoverflow.com/questions/5985468/iphone-differences-among-time-zone-convenience-methods