iOS 5 Date Selected in DatePicker

1k views Asked by At

I want to have in my DatePicker selected date, but I have a problem with lag of 2 hours.

 // String From Date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateSelected = [datePicker date];
NSString *dateChoisie = [dateFormatter stringFromDate:dateSelected];
NSDate *dateFinal = [dateFormatter dateFromString:dateChoisie];

NSLog(@"dateSelected = %@", dateSelected);
NSLog(@"dateChoisie = %@", dateChoisie);
NSLog(@"dateFinal = %@", dateFinal);

Result :

2011-10-15 00:16:11.129 QuickEvent[25459:707] dateSelected = 2011-10-14 22:20:05 +0000
2011-10-15 00:16:11.132 QuickEvent[25459:707] dateChoisie = 2011-10-15 00:20:05 +0200
2011-10-15 00:16:11.138 QuickEvent[25459:707] dateFinal = 2011-10-14 22:20:05 +0000
1

There are 1 answers

0
Arun On

Try using this kind of formatting..

NSTimeZone *sourceTimeZone =[NSTimeZone systemTimeZone];
NSTimeZone *destinationTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:(TIME_ZONE_THAT_YOU_WANTED*3600)];

NSInteger sourceSeconds =[sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationSeconds =[destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationSeconds - sourceSeconds;

NSDate *convertedDate =[[[NSDate alloc]initWithTimeInterval:interval sinceDate:date] autorelease];
return convertedDate;

For an example you can apply TIME_ZONE_THAT_YOU_WANTED = -8 for New york timezone based time

Thanks, Arun.