0
NSDate *localDate = [NSDate date];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:localDate];
NSDate *gmtDate = [localDate dateByAddingTimeInterval:-timeZoneOffset];

This gives me time in 12 hours format. So, for example it is is 2.15 pm. It gives me 2016-01-16 02:15:00 +0000.

However, I want in 24 hours format like 14:15:00. Also I want just the time. How can I get that.

Natasha
  • 6,651
  • 3
  • 36
  • 58
  • Please have a look at the code. I find no way to convert a date from 12 hours to 24 hours using date formatter. I needed my time to be in GMT. It's is not that i didn't do research. I just didn't find the right direction. If I am doing anything wrong then at least point me to the right direction. Right now, doing NSDate *now =[NSDate date]; is giving me 2016-01-16 08:46:33 +0000 which is nothing near 2:50 pm. So, if there is another way then please suggest that. – Natasha Jan 16 '16 at 08:51

1 Answers1

1

If you need a string that represents time in the format you need, try use NSDateFormatter with setting it GMT timeZone and desired dateFormat (HH:mm:ss in your case)

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSLog(@"Current time in GMT = %@", [formatter stringFromDate:[NSDate date]]);

This would print time in format like 12:34:56 in GMT timezone

For more info on about date formats see this fine article

arrteme
  • 393
  • 1
  • 3
  • 14