- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
{
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];
return destinationDateNow;
}
例子演示:我的机器是北京时区东八区。
//2013-08-03T12:53:51+0800 UTC时间格式下的北京时间,可以看到北京时间= UTC + 8小时。
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *localDate = [dateFormatter
dateFromString:@"2013-08-03T04:56:52+0000"]; +0000 表示的是当前时间是个世界时间。
[dateFormatter release];
NSLog(@"now Time = %@",[selfgetNowDateFromatAnDate:localDate]);
结果:
2013-08-03 12:57:33.391 xxxx[2547:c07] now Time = 2013-08-03 12:56:52 +0000
以上注意一点,在转出来后带的时间是原参数anydate的时区,因此切不可再用NSDateFormatter 转换。否则会多增加一个时区的时间值。应该使用如下来提取字符串
NSString *str = [NSStringstringWithFormat:@"%@",[selfgetNowDateFromatAnDate:localDate]];
NSLog(@"str = %@",str);
注NSDate对象存放的日期始终是UTC的标准时间,可以根据这个时间进行其它时间的转换。因此上面算出来的时间中时区为 +0000,如果此时再转为字符串
几个转换函数
//NSString 2 NSDate
- (NSDate *)stringToDate:(NSString *)strdate
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *retdate = [dateFormatter
dateFromString:strdate];
[dateFormatter release];
return retdate;
}
//NSDate 2 NSString
- (NSString *)dateToString:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter
stringFromDate:date];
[dateFormatter release];
return strDate;
}
//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
-(NSString *)getUTCFormateLocalDate:(NSString *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
//将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
-(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localTimeZone];
NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
- (NSString *)getUTCFormatDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
NSTimeZone *timeZone = [NSTimeZonetimeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateString = [dateFormatter
stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
- (NSDate *)getLocalFromUTC:(NSString *)utc
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]
init];
NSTimeZone *timeZone = [NSTimeZonelocalTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *ldate = [dateFormatter
dateFromString:utc];
[dateFormatter release];
return ldate;
}
//以上注意字符串时的输入参数的格式,别外不要用%@来查看NSDate的值,因为本身存的就是UTC ,小心被误倒。将期转换出字符串来查看一下。