Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get current NSDate same to simulator Date? iPhone Sdk

Tags:

iphone

I am trying to get current date with adding device time zone but is show 1 hr late that original date. I thing , I am getting problem of DaylightSavingTime. How to disable isDaylightSavingTime = FALSE .

here is the code, I have used..

NSDate *date = [NSDate Date];
NSTimeZone *currentTimeZon = [NSTimeZone localTimeZone];
if ([currentTimeZon isDaylightSavingTime]) 
{
    NSLog(@"East coast is NOT on DT");
} 
else 
{
    NSLog(@"East coast is on DT");  

}
NSTimeInterval timeZoneOffset = [currentTimeZon secondsFromGMTForDate:currentDate];
NSTimeInterval gmtTimeInterval = [date timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSLog(@"### Announcement gmtDate = %@",gmtDate);

I am getting time with 1 Hour difference, date is perfact.

like image 423
Sujal Avatar asked Aug 17 '11 11:08

Sujal


1 Answers

Use NSCalendar, it understands time zones, daylight savings, etc. As @albertamg says, NSDate is just the time since a reference at UTC (GMT), it has no other concept.

like image 129
zaph Avatar answered Oct 13 '22 17:10

zaph