1

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
jay
  • 3,517
  • 5
  • 26
  • 44
  • I think this is what you need: http://stackoverflow.com/questions/16254575/how-do-i-get-iso-8601-date-in-ios – sCha Feb 12 '14 at 05:13
  • @Scha I already referred the post you mentioned. But my question is how can I convert it to a format like this: 09:30p.m ? – jay Feb 12 '14 at 05:15

2 Answers2

2

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];
Rajjjjjj
  • 424
  • 3
  • 16
2

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
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30