ios时间时区处理

来源:互联网 发布:电脑桌面日程软件 编辑:程序博客网 时间:2024/05/22 07:59
  1. - (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate  
  2. {  
  3.     //设置源日期时区  
  4.     NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT  
  5.     //设置转换后的目标日期时区  
  6.     NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];  
  7.     //得到源日期与世界标准时间的偏移量  
  8.     NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];  
  9.     //目标日期与本地时区的偏移量  
  10.     NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];  
  11.     //得到时间偏移量的差值  
  12.     NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;  
  13.     //转为现在时间  
  14.     NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];  
  15.     return destinationDateNow;  
  16. }  


NSDateFormatter *dateformatter = [[NSDateFormatteralloc]init];

    [dateformattersetDateFormat:@"yyyy-MM-dd hh:mm:ss"];

   NSString *dateStr = [dateformatter stringFromDate:date];


0 0
原创粉丝点击