简体   繁体   中英

Convert NSString to NSDate and timeZone?

Ive been racking my brains with no luck. Could someone please tell my how i would convert this string:

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

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

into a NSDate ? and time format ?

how can i covert this date into time format ?

here is my code:

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];

in console

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

i have time in miliseconds then how can i take out minutes/hours/ ?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM