From server I'm receiving GMT time structure(user defined structure), using that I want to convert it into local time, I have done it by filling the NSDatecomponent with the structure received, and then I have used date formattter to get date from it, everything works fine except one case. If the GMT time is after Nov 3 (Daylight Saving Time change in US) formatter is producing 1 hour time difference.
for eg: If the intended time is for Nov-3 ,4 PM, after conversion from GMT to local its giving Nov-3, 3 PM.
Any idea how to avoid it.
Edit:
// Selected Dates
NSDateComponents *sel_date = [[NSDateComponents alloc]init];
sel_date.second = sch_detail.sel_dates.seconds;
sel_date.minute = sch_detail.sel_dates.mins;
sel_date.hour = sch_detail.sel_dates.hours;
sel_date.day = sch_detail.sel_dates.date;
sel_date.month = sch_detail.sel_dates.month;
sel_date.year = sch_detail.sel_dates.year;
sel_date.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
// Get the Date format.
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// Start_date formatter.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *strt_date_loc = [gregorian dateFromComponents:sel_date];
// Get date string.
NSString *sel_date_time = [dateFormatter stringFromDate: strt_date_loc];+
sel_date_time string is one hour less than what it supposed to be..
Log :
strt_date_loc = 2013-11-30 06:56:00 +0000
sel_date_time = Nov 29, 2013 10:56 PM (but it should be 11:56 PM)
TimeZone : Palo Alto (US)
Local to gmt Conversion:
- (NSDateComponents*) convert_to_gmt_time : (NSDate*) date
{
NSDate *localDate = date;
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSDateComponents *date_comp = [[NSCalendar currentCalendar] components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:gmtDate];
return date_comp;
}
thanx.