Natasha
Natasha

Reputation: 6893

How can I get the date in 24 hours format

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.

Upvotes: 0

Views: 610

Answers (1)

arrteme
arrteme

Reputation: 393

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

Upvotes: 1

Related Questions