Reputation: 3667
I have a time string in the following format(ISO 8601
):
start = "1900-01-01T11:30:00"
stop = 1900-01-01T21:30:00
From the above string I need to display time like this :
Starting Time : 11:30a.m
Stopping Time : 09:30p.m
For that I had created an NSDate using below code, But I think it displays time in GMT format.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
date = [dateFormatter dateFromString:opening];
stopTime = date;
//Output of above code : Date = 1900-01-01 15:36:40 +0000
How can I obtain the Starting and Ending time string from above ISO 8601
time string? Do I need to depend on String operations rather than using iOS NSDateformatter
?
Upvotes: 1
Views: 108
Reputation: 5182
Try this,
NSString *start = @"1900-01-01T11:30:00";
NSString *stop = @"1900-01-01T21:30:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *sDate = [dateFormatter dateFromString:start];
NSDate *eDate = [dateFormatter dateFromString:stop];
[dateFormatter setDateFormat:@"hh:mm a"];
start = [dateFormatter stringFromDate:sDate];
stop = [dateFormatter stringFromDate:eDate];
NSLog(@"Starting Time : %@", start);
NSLog(@"Stopping Time : %@", stop);
And the outputs will be
2014-02-12 10:55:13.931 [950:a0b] Starting Time : 11:30 AM
2014-02-12 10:55:13.932 [950:a0b] Stopping Time : 09:30 PM
Upvotes: 2
Reputation: 434
Check this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *currentDate = [NSDate date];
NSString *iso8601valueStr = [dateFormatter stringFromDate:currentDate];
Upvotes: 2