I’m having an issue converting minutes since midnight to a correct time. It works fine until i reach noon. At which point the time becomes 0:45AM and anything after noon is blank. Am I missing something in my format?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"h:m"];
NSLog(@"Minutes %i",minutes);
NSString *time = [NSString stringWithFormat:@"%i:%i", (minutes / 60), (minutes % 60)];
NSLog(@"time: %@",time);
NSDate *formatTime = [dateFormatter dateFromString:time];
NSLog(@"formatTime %@",formatTime);
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
[dateFormatter setDateFormat:@"H:mma"];
NSString *newTime = [dateFormatter stringFromDate:formatTime];
NSLog(@"NewTime: %@",newTime);
dateFormatter = nil;
Log
2012-01-25 15:12:23.194 MyApp[43875:f803] Minutes 480
2012-01-25 15:12:23.194 MyApp[43875:f803] time: 8:0
2012-01-25 15:12:23.195 MyApp[43875:f803] formatTime 1970-01-01 15:00:00 +0000
2012-01-25 15:12:23.196 MyApp[43875:f803] NewTime: 8:00AM
2012-01-25 15:12:23.197 MyApp[43875:f803] Minutes 495
2012-01-25 15:12:23.197 MyApp[43875:f803] time: 8:15
2012-01-25 15:12:23.198 MyApp[43875:f803] formatTime 1970-01-01 15:15:00 +0000
2012-01-25 15:12:23.199 MyApp[43875:f803] NewTime: 8:15AM
2012-01-25 15:12:23.200 MyApp[43875:f803] Minutes 765
2012-01-25 15:12:23.200 MyApp[43875:f803] time: 12:45
2012-01-25 15:12:23.201 MyApp[43875:f803] formatTime 1970-01-01 07:45:00 +0000
2012-01-25 15:12:23.201 MyApp[43875:f803] NewTime: 0:45AM
2012-01-25 15:12:23.203 MyApp[43875:f803] Minutes 480
2012-01-25 15:12:23.203 MyApp[43875:f803] time: 8:0
2012-01-25 15:12:23.204 MyApp[43875:f803] formatTime 1970-01-01 15:00:00 +0000
2012-01-25 15:12:23.204 MyApp[43875:f803] NewTime: 8:00AM
2012-01-25 15:12:23.205 MyApp[43875:f803] Minutes 495
2012-01-25 15:12:23.206 MyApp[43875:f803] time: 8:15
2012-01-25 15:12:23.206 MyApp[43875:f803] formatTime 1970-01-01 15:15:00 +0000
2012-01-25 15:12:23.207 MyApp[43875:f803] NewTime: 8:15AM
2012-01-25 15:12:23.208 MyApp[43875:f803] Minutes 555
2012-01-25 15:12:23.209 MyApp[43875:f803] time: 9:15
2012-01-25 15:12:23.209 MyApp[43875:f803] formatTime 1970-01-01 16:15:00 +0000
2012-01-25 15:12:23.210 MyApp[43875:f803] NewTime: 9:15AM
2012-01-25 15:12:23.211 MyApp[43875:f803] Minutes 630
2012-01-25 15:12:23.211 MyApp[43875:f803] time: 10:30
2012-01-25 15:12:23.212 MyApp[43875:f803] formatTime 1970-01-01 17:30:00 +0000
2012-01-25 15:12:23.212 MyApp[43875:f803] NewTime: 10:30AM
2012-01-25 15:12:23.214 MyApp[43875:f803] Minutes 690
2012-01-25 15:12:23.214 MyApp[43875:f803] time: 11:30
2012-01-25 15:12:23.215 MyApp[43875:f803] formatTime 1970-01-01 18:30:00 +0000
2012-01-25 15:12:23.215 MyApp[43875:f803] NewTime: 11:30AM
2012-01-25 15:12:23.217 MyApp[43875:f803] Minutes 765
2012-01-25 15:12:23.217 MyApp[43875:f803] time: 12:45
2012-01-25 15:12:23.217 MyApp[43875:f803] formatTime 1970-01-01 07:45:00 +0000
2012-01-25 15:12:23.218 MyApp[43875:f803] NewTime: 0:45AM
2012-01-25 15:12:32.506 MyApp[43875:f803] Minutes 840
2012-01-25 15:12:32.507 MyApp[43875:f803] time: 14:0
2012-01-25 15:12:32.507 MyApp[43875:f803] formatTime (null)
2012-01-25 15:12:32.508 MyApp[43875:f803] NewTime: (null)
EDIT:
I am using Munid’s Edit 2 below and it works well except it seems to be subtracting 7 hours from newDate when I run it through the dateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
[dateFormatter setDateFormat:@"hh:mm a"];
NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
NSLog(@"midnight %@",midnight);
NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];
NSLog(@"new date %@", newDate);
//[dateFormatter setDateFormat:@"hh:mma"];
NSString *newTime = [dateFormatter stringFromDate:newDate];
NSLog(@"Time: %@", newTime);
dateFormatter = nil;
will log
2012-01-25 16:15:03.418 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.418 MyApp[44721:f803] new date 1970-01-01 08:00:00 +0000
2012-01-25 16:15:03.419 MyApp[44721:f803] Time: 01:00 AM
2012-01-25 16:15:03.420 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.420 MyApp[44721:f803] new date 1970-01-01 08:15:00 +0000
2012-01-25 16:15:03.420 MyApp[44721:f803] Time: 01:15 AM
2012-01-25 16:15:03.421 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.422 MyApp[44721:f803] new date 1970-01-01 12:45:00 +0000
2012-01-25 16:15:03.422 MyApp[44721:f803] Time: 05:45 AM
2012-01-25 16:15:03.423 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.424 MyApp[44721:f803] new date 1970-01-01 08:00:00 +0000
2012-01-25 16:15:03.424 MyApp[44721:f803] Time: 01:00 AM
2012-01-25 16:15:03.425 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.425 MyApp[44721:f803] new date 1970-01-01 08:15:00 +0000
2012-01-25 16:15:03.426 MyApp[44721:f803] Time: 01:15 AM
2012-01-25 16:15:03.427 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.427 MyApp[44721:f803] new date 1970-01-01 09:15:00 +0000
2012-01-25 16:15:03.427 MyApp[44721:f803] Time: 02:15 AM
2012-01-25 16:15:03.428 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.429 MyApp[44721:f803] new date 1970-01-01 09:30:00 +0000
2012-01-25 16:15:03.429 MyApp[44721:f803] Time: 02:30 AM
2012-01-25 16:15:03.430 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.431 MyApp[44721:f803] new date 1970-01-01 10:30:00 +0000
2012-01-25 16:15:03.431 MyApp[44721:f803] Time: 03:30 AM
2012-01-25 16:15:03.432 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.432 MyApp[44721:f803] new date 1970-01-01 11:30:00 +0000
2012-01-25 16:15:03.433 MyApp[44721:f803] Time: 04:30 AM
First, maybe your method with format strings is a little bit too involved. Don’t forget, that the
NSLogoutput also needs to be formatted, otherwise you will get a different locale and time zone, so these values are not reliable for checking if your logic is right.The simple solution is to use the fact that
NSTimeIntervalis in seconds. Thus:EDIT:
Also, I checked your code – you can fix it by changing the format strings. The problem was that the date formatter got confused with the minute having 1 or 2 digits.
EDIT2:
Expanding on the code above as requested: