转换iOS中系统时区中的任何时区日期

问题描述:

我在将给定日期转换为iOS系统时区时遇到问题。转换iOS中系统时区中的任何时区日期

来源日期:2013-03-18 03:54:18 //其AM格式 目的日期应为:2013-03-18 03:30:15 //其PM格式。

我怎样才能得到这个?

我试过下面的代码,但得到错误的数据和差异了。

NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 
NSLog(@"Post date::%@",postDate); 
NSDate *utc = [fmt dateFromString:postDate]; 
fmt.timeZone = [NSTimeZone systemTimeZone]; 
NSString *local = [fmt stringFromDate:utc]; 
NSLog(@" local %@", local); 

NSDate *date1 = [NSDate date]; 
NSString *currentDateString = [fmt stringFromDate:date1]; 
NSDate *currentDate = [fmt dateFromString:currentDateString]; 
NSUInteger flags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; 
NSDateComponents *difference = [[NSCalendar currentCalendar] components:flags fromDate:utc toDate:currentDate options:0]; 
NSLog(@"difference::%d",[difference day]); 
NSLog(@"difference::%d",[difference hour]); 
NSLog(@"difference::%d",[difference minute]); 

编辑:输出

Post date::2013-03-18 03:20:09 
local 2013-03-18 03:20:09 
difference::0 
difference::13 
difference::2 
+1

显示nslogs ... – 2013-03-18 11:14:39

+0

发布时间:: 2013年3月18日03:20: 09 当地2013年3月18日3时20分09秒 差异:: 0 差异:: 13 差异:: 2 – 2013-03-18 11:17:45

+0

显示为对象的NSDate不是字符串 – 2013-03-18 11:22:32

NSString *openingHour = @"15:05:35 "; 
NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init]; 
     NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; 
     [workingHoursFormatter setTimeZone:timeZone]; 
     [workingHoursFormatter setDateFormat:@"HH:mm"]; 
     NSDate *openingTime = [workingHoursFormatter dateFromString:openingHour]; 

你可以尝试对我的作品上面的代码,更改数据格式,您的需要

+0

openingHour代表什么? – Ganapathy 2013-03-18 11:41:35

+0

我更新答案你现在可以检查! – Vinodh 2013-03-18 11:46:24

+0

感谢它帮助我很多.... – 2013-03-18 12:04:39

替换从您的代码...

fmt.timeZone = [NSTimeZone systemTimeZone]; 

像这样

fmt.timeZone = [NSTimeZone localTimeZone]; 

这将会为你工作。 ..

+0

做完上述变更后,我开始g以下的结果。发布日期:: 2013-03-18 03:54:18和当地2013-03-18 03:54:18这是错误的....我想在我的系统时区的日期和时间....但它在此之后显示错误..... – 2013-03-18 11:26:26