Reputation: 680
Assume that I have string like this from server @"2014-03-08T16:59+0000".
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [self dateFromJSONString:@"2014-03-08T16:59+0000"];
NSDateComponents *dateComponents = [calendar components:(NSTimeZoneCalendarUnit) fromDate:date];
NSLog(@"TimeZone is %@", [[dateComponents timeZone] abbreviation]);
But the TimeZone is not base on the string, it based on the currentTimeZone of the device.
Is it possible to extract timeZone from string ?
Upvotes: 1
Views: 1493
Reputation: 18253
If you are sure that the string you need to parse is always formatted in that way, then regexes provide a simple way to do it:
NSString *str = @"2014-03-08T16:59+0000";
NSString *pattern = @"^.*T\\d{2}:\\d{2}";
NSString *timezone = [str stringByReplacingOccurrencesOfString: pattern
withString: @""
options: NSRegularExpressionSearch
range: NSMakeRange(0, str.length)];
Upvotes: 1
Reputation: 16864
-(NSArray*)convertToLocalDate:(NSString*)dateStr{
NSArray *convert;
NSString *time=@"";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
//NSLog(@"date : %@",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] ;
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd.MM.yyyy"];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatters stringFromDate: destinationDate];
NSDateFormatter *dateFormatters1 = [[NSDateFormatter alloc] init];
[dateFormatters1 setDateFormat:@"hh:mm a"];
[dateFormatters1 setTimeZone:[NSTimeZone systemTimeZone]];
time = [dateFormatters1 stringFromDate: destinationDate];
convert = [[NSArray alloc ]initWithObjects:dateStr,time,nil];
return convert;
}
Upvotes: 1
Reputation: 10195
You have to parse the offset from GMT from your string yourself.
Something like this, but you'll have to tweak for your slightly different format:
/**
This is assuming format yyyy-MM-dd'T'HH:mm:ssZZZZZ . ie the last 5 chars are timezone offset from gtm in the form (+|-)##:##
*/
-(NSTimeZone*)timezoneFromDateString:(NSString*)dateString {
NSTimeZone *timezone = nil;
NSString *timezoneComponent = [dateString substringFromIndex:19];
if(timezoneComponent.length == 6) {
NSArray *components = [[timezoneComponent substringFromIndex:1] componentsSeparatedByString:@":"];
NSInteger offset = [[timezoneComponent substringToIndex:1] isEqualToString:@"-"] ? -1 : 1;
if(components.count == 2) {
offset *= [components[0] integerValue] * 60*60 + [components[1] integerValue] *60;
timezone = [NSTimeZone timeZoneForSecondsFromGMT:offset];
}
}
return timezone;
}
Upvotes: 2