double duration = 20; // duration is in seconds
NSDate* durationDate = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:duration];
NSDateFormatter *formatter = nil;
formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"mm:ss"];
NSString *durationString=[formatter stringFromDate:durationDate];//here durationString should have 20
but i am getting 30:20? from where this 30 min is getting added. I have tried giving bigger numbers then also same result
please let me know thoughts on this and how to solve this
You’re probably on India Standard Time?
The reference date is defined to be at midnight GMT. IST is GMT+5:30.
So when you add 20 seconds to the reference date you get 20 seconds past midnight in GMT. When you then create a date formatter and don’t set anything else to it, it’ll operate in your device’s natural time zone. That means that when you ask it for the minutes and seconds at the end of the time you get 30 minutes and 20 seconds.
NSDates are an absolute time, abstract of any time zone or calendar. If five devices in five separate time zones all call[NSDate date]simultaneously to get the current time, they’d all get objects with the same value.NSDateFormattercombines anNSDatewith anNSCalendarand anNSTimeZoneto determine minutes, seconds, hours, months, years, etc.