Reputation: 331
My local and default time zone is GMT +5 but when I get date and time by NSDate it return me GMT date and time.
For example the code and output from my code while testing on device is as, [device time zone Islamabad GMT +5]
NSTimeZone *lo = [NSTimeZone localTimeZone];
NSLog(@" - current local timezone is %@",lo); // GMT +5
2010-12-28 20:56:11.785 Done[484:307] - current local timezone is Local Time Zone (Asia/Karachi (GMT+05:00) offset 18000)
NSTimeZone *df = [NSTimeZone defaultTimeZone];
NSLog(@" - current default timezone is %@",df); // GMT +5
2010-12-28 20:56:11.790 Done[484:307] - current default timezone is Asia/Karachi (GMT+05:00) offset 18000
but
NSDate *cDate = [NSDate date];
NSLog(@"current date by NSDate %@",cDate); //but NSDate show GMT
2010-12-28 20:56:11.794 Done[484:307] current date by NSDate 2010-12-28 15:56:11 GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
//// NSTimeZone *gmt = [NSTimeZone ]
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+05:00"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@" date string object %@" ,timeStamp); // string From Date is GMT +5
2010-12-28 20:56:11.802 Done[484:307] date string object 2010-12-28T20:56
NSDate *datef = [dateFormatter dateFromString:timeStamp];
NSLog(@" date object %@" ,datef); // the date form above string gives again GMT
2010-12-28 20:56:11.809 Done[484:307] **date object 2010-12-28 15:56:00 GMT**
Why is NSDate not giving local current time? Please help...
Upvotes: 33
Views: 49148
Reputation: 30571
Forget NSDateFormatter - it's way too complicated, instead try this magic:
NSLog(@"Date with local timezone is: %@",
[date descriptionWithLocale:NSLocale.systemLocale]);
Upvotes: 24
Reputation: 158
Try this
- (NSDate *) GetLocalDate {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
NSCalendar *theCalendar = [NSCalendar currentCalendar];
theCalendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
dayComponent = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSDate *localDate = [theCalendar dateFromComponents:dayComponent];
return localDate;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"localDate : %@",[self GetLocalDate]);
}
Upvotes: 4
Reputation: 593
Eliseo Chavez Jr answer translated to Swift
let sourceDate = NSDate();
let sourceTimeZone = NSTimeZone(abbreviation: "GMT")!;
let destinationTimeZone = NSTimeZone.systemTimeZone();
let sourceGMTOffset: NSInteger = sourceTimeZone.secondsFromGMTForDate(sourceDate);
let destinationGMTOffset:NSInteger = destinationTimeZone.secondsFromGMTForDate(sourceDate);
let interval: NSTimeInterval = Double (destinationGMTOffset - sourceGMTOffset);
let destinationDate = NSDate(timeInterval: interval, sinceDate: sourceDate);
Upvotes: 2
Reputation: 6082
This works well for me to get the date in my timezone and format it just like I want it:
NSDate* gmtDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:gmtDate];
Upvotes: 1
Reputation: 2405
If you want local Date and time. Try this code:-
NSString *localDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
Upvotes: 3
Reputation: 854
Try this...
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
This help respond to the current system timezone.
Upvotes: 72
Reputation: 75058
NSDate is a "raw" date. That's why it is in GMT. It's up to the code to use NSDateFormatter (as you have done) to output the date to a value that makes sense for the user.
In some rare cases you might need to display an NSDate not using the users time zone (like if you want to display a time in New York time no matter where the user is). Then, you set the time zone on the date formatter to a specific value (again, as you have done).
It's common practice for computers to store all dates in GMT, and then adjust how they are displayed for the user. If you start trying to alter how the date is actually stored you are going to mess up a lot of date handling frameworks that are all assuming your NSDate is in GMT.
As you have seen, when you read in a date via an NSDateFormatter, it's converted from the time entered based on the timezone of the formatter you have set, and then converted to GMT. So what do you think is wrong with what it is doing? Because it's doing just what it should - storing dates in GMT and outputting strings based on a timezone.
Upvotes: 8