将NSString转换为NSDate和timeZone?

时间:2014-04-09 04:45:06

标签: objective-c nsstring nsdate nsdateformatter

我一直绞尽脑汁没有运气。有人可以告诉我如何转换这个字符串:

 NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"date_time"];

2014-04-13T17:00:00 + 11:0

进入NSDate?和时间格式?

我怎样才能将这个日期转换为时间格式?

这是我的代码:

NSString *dateTimeStr =[[self.responseArray objectAtIndex:indexPath.row]objectForKey:@"date_time"];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

[dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];

NSDate *date = [dateFormatter dateFromString:dateTimeStr];

NSLog(@"date %@", date);

NSTimeInterval seconds = [date timeIntervalSinceReferenceDate];

在控制台

NSLog(@"seconds==> %f", seconds);

我有几分钟的时间,那我怎么能拿出minutes/hours/

1 个答案:

答案 0 :(得分:3)

NSString *dateString = @"2014-04-13T17:00:00+11:00";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *date = [dateFormatter dateFromString:dateString];

NSDateComponents *dateComponents = [[NSCalendar currentCalendar]
                                    components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                    fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];