6

I just noticed that NSDate *nowDate = [NSDate date]; gives me GMT+0 Time and not the local time. So basically on my iPad it's 13:00 and the output of this code is 12:00.

How do I get local time properly?

Jacek Kwiecień
  • 12,045
  • 20
  • 83
  • 148
  • Try this link, it allows you to get local time zone. http://buildingmyworld.wordpress.com/2011/04/13/get-local-time-from-iphone-local-nsdate/ – Adrian P Mar 25 '13 at 13:14
  • How are you getting the "output of this code"? Are you using an NSDateFormatter to get a string or are you just `NSLog`ging the NSDate object? – colincameron Mar 25 '13 at 16:34

4 Answers4

28

Give it a Shot !

NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];//use `[NSTimeZone localTimeZone]` if your users will be changing time-zones. 

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

It will give you the time according to the current system timezone.

Albert Renshaw
  • 16,406
  • 17
  • 99
  • 182
Rajan Balana
  • 3,766
  • 23
  • 42
6

NSDate does not care about timezones. It simply records a moment in time.

You should set the local timezone when using the NSDateFormatter to get a string representation of the date:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Set date and time styles
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *dateString = [dateFormatter stringFromDate:date];
colincameron
  • 2,619
  • 4
  • 22
  • 46
  • and then what? Should I convert NSSTring to NSDate again? Doesn't look much efficient – Jacek Kwiecień Mar 26 '13 at 08:47
  • 1
    If you need an NSDate then just use `[NSDate date]` - it doesn't care about timezones. I suggest you read the Date and Time Programming Guide - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i – colincameron Mar 26 '13 at 10:10
  • Thats the problem I need an NSDate object that cares ;) – Jacek Kwiecień Mar 26 '13 at 13:58
  • 1
    Needs an additional square bracket in the alloc/init line and change NSDateFormatter to dateFormatter in final line. SO wouldn't allow changes less than 6 characters – amergin Sep 05 '15 at 10:49
  • @amergin thanks - edited – colincameron Jan 14 '16 at 14:36
5
 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
 [calendar setTimeZone:[NSTimeZone localTimeZone]];
 NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
 NSDate *d = [calendar dateFromComponents:dateComponents];
wrtsprt
  • 5,157
  • 4
  • 19
  • 28
ahwulf
  • 2,574
  • 14
  • 28
4
// get current date/time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(@"%@",currentTime);
iPatel
  • 43,583
  • 14
  • 113
  • 135