我从服务器接收字符串形式的日期,我需要根据我的时区(印度:GMT +5:30)显示日期。这是我的代码
NSString *dateString = @"2015-08-10 11:45:10";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(@"date = %@", localDateString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSDate *date2 = [dateFormatter2 dateFromString:localDateString];
NSLog(@"%@",date);
日志是:
2015-08-10 11:45:10 +0000 (NSDate)
2015-08-10 11:45:10 (NSString)
date = 2015-08-10 17:15:10 (NSString)
2015-08-10 11:45:10 +0000 (NSDate)
我的问题是最后一个日志 (2015-08-10 11:45:10 +0000) 为什么不是2015-08-10 17:15:10 ??