8

How do I get GMT time?

NSDate *c =[NSDate date];

gives system time, not GMT.

cobbal
  • 69,903
  • 20
  • 143
  • 156
g.revolution
  • 11,962
  • 23
  • 81
  • 107

7 Answers7

9

This is a simpler version of Ramin's answer

+ (NSDate *) GMTNow
{ 
 NSDate *sourceDate = [NSDate date];
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];

    [sourceDate addTimeInterval:currentGMTOffset];

    return sourceDate;
}
Chris
  • 2,727
  • 2
  • 27
  • 28
  • 1
    This is wrong though. If I am in NY currentGMOffset will be -14400. Since GMT is later than NYC, I want to add a positive value to sourceDate. Also sourceDate has not been modified here. You need sourceDate = [sourceDate addTimeInterval:currentGMTOffset*-1]; which inverts the sign of the offset. – ransomweaver Nov 04 '10 at 19:00
  • what if the device time is 2 hours late .in that case will this calculation is correct??? – Shaik Riyaz Sep 17 '13 at 09:53
8

If it's for display purposes you want to display it, use NSDateFormatter like so:

NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

// Set date style:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
Barry Hurley
  • 567
  • 1
  • 5
  • 18
  • what if the device time is 2 hours late .in that case will this calculation is correct??? – Shaik Riyaz Sep 17 '13 at 09:53
  • How would you know if the device is 2 hours late? You can only work with what you're given and must rely on the system time being correct for any cases. This is the system's job, not your app. – Barry Hurley Sep 18 '13 at 10:58
  • we have to consider all scenarios , my scenario is i want to get gmt time independent of device time . . . is there any other way to achieve this ? – Shaik Riyaz Sep 18 '13 at 11:42
  • I think you'll have to call some api's to get the current gmt time ,so that you'll get correct time irrespective of the device used. – Suraj K Thomas Mar 31 '14 at 14:25
4

If performing date calculations, these categories may be useful. Having converted your date to being 'normalized' (that is, having the same month, day, and year but at +1200 UTC), if you then perform subsequent calculations using an NSCalendar that is also set to UTC (+[NSCalendar normalizedCalendar]), it'll all work out.

@implementation NSDate (NormalizedAdditions)

+ (NSDate *)normalizedDateFromDateInCurrentCalendar:(NSDate *)inDate
{
    NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                                                        fromDate:inDate];
    [todayComponents setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [todayComponents setHour:12];

    return [[NSCalendar normalizedCalendar] dateFromComponents:todayComponents];
}

+ (NSDate *)normalizedDate
{
    return [self normalizedDateFromDateInCurrentCalendar:[NSDate date]];
}

@end

@implementation NSCalendar (NormalizedAdditions)

+ (NSCalendar *)normalizedCalendar
{
    static NSCalendar *gregorian = nil;
    if (!gregorian) {
        gregorian = [[NSCalendar alloc]
                     initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];   
    }
    return gregorian;
}

@end
Evan Schoenberg
  • 943
  • 8
  • 7
2
+ (NSDate*) convertToGMT:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];

    NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];     
    return destinationDate;
}
Bill Wynne
  • 53
  • 5
  • 1
    This is actually a very good utility method. I will use it in my current project. However it does not really answer the question. The question was "How do I get GMT time?" – Johan Karlsson Nov 07 '12 at 12:26
0

NSDate stores the time zone internally -- there are a few functions you can call and pass in a target timezone if you jsut want a string representation of the date, see apple's documentation

olliej
  • 35,755
  • 9
  • 58
  • 55
0

See:

CFTimeZoneCreateWithTimeIntervalFromGMT secondsFromGMT

Date & Time Programming Guide

mahboudz
  • 39,196
  • 16
  • 97
  • 124
-4
- (NSDate*) convertToUTC:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease]; 
    return destinationDate;
}
Ramin
  • 13,343
  • 3
  • 33
  • 35
  • 2
    -1 This `NSDate` to `NSDate` conversion is misleading and based on a misconception of what a `NSDate` object represents. – albertamg Aug 19 '11 at 13:48
  • @Dave DeLong I agree that trying to convert `NSDate` objects between timezones is nonsense and I don't get why this other answer has so many upvotes:http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device/1082179#1082179 – albertamg Aug 19 '11 at 17:01
  • 6
    This answer is just wrong. NSDate objects represent a single instant in time. They don't care anything about the time zone. The time zone only comes into play when you're formatting and displaying the date. Modifying the NSDate object means that you're modifying the point in time that it represents. – Bryan Henry Sep 28 '11 at 20:50
  • @NickForge, What is correct solution for converting device time to GMT time? Other answers are calculating string but I want a GMT date not a GMT timestring (as in following answer NSString *GMTDateString = [dateFormatter stringFromDate: myDate]; ) – chatur Apr 02 '12 at 15:03
  • @chatur Read Sbrocket's comment above. – Nick Forge Apr 03 '12 at 01:21
  • what if the device time is 2 hours late .in that case will this calculation is correct??? – Shaik Riyaz Sep 17 '13 at 09:52
  • Available in Mavericks: - (NSDateComponents *)componentsInTimeZone:(NSTimeZone *)timezone fromDate:(NSDate *)date – the Reverend Jan 01 '14 at 00:11