Objective-C:将分钟转换为时间

时间:2012-01-25 22:10:32

标签: iphone objective-c ios5

我有一个问题,从午夜到正确的时间将分钟转换。它工作正常,直到我到达中午。此时,时间变为0:45AM,中午之后的任何内容都为空白。我错过了格式的东西吗?

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;

日志

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)

编辑: 我正在使用下面的Munid的编辑2,它运行良好,但是当我通过newDate

运行时,dateFormatter似乎减去了7个小时
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;

将记录

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

2 个答案:

答案 0 :(得分:3)

首先,也许你的格式字符串方法有点过于复杂。不要忘记,NSLog输出也需要格式化,否则您将获得不同的区域设置和时区,因此这些值对于检查逻辑是否正确是不可靠的。

简单的解决方案是使用NSTimeInterval在几秒钟内的事实。因此:

NSDate *midnight; // the date you want, at 0:00 am
NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];

编辑:

另外,我检查了你的代码 - 你可以通过更改格式字符串来修复它。问题是日期格式化程序与具有1或2位数的分钟混淆。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
for (NSInteger minutes = 495; minutes < 1000; minutes +=60) {
    [dateFormatter setDateFormat:@"hh:mm"];
    NSLog(@"Minutes %2i",minutes);
    NSString *time = [NSString stringWithFormat:@"%2i:%2i", (minutes / 60), (minutes % 60)];
    NSLog(@"time: %@",time);
    NSDate *formatTime = [dateFormatter dateFromString:time];
    NSLog(@"formatTime %@",formatTime);
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
    [dateFormatter setDateFormat:@"HH:mm a"];
    NSString *newTime = [dateFormatter stringFromDate:formatTime];
    NSLog(@"NewTime: %@",newTime);
}
dateFormatter = nil;        

<强> EDIT2:

按要求扩展上面的代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
for (NSInteger minutes = 495; minutes < 1000; minutes +=60) {
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];
    NSLog(@"newDate: %@", [dateFormatter stringFromDate:newDate]);
}
dateFormatter = nil;        

答案 1 :(得分:2)

这是一个正确处理用户区域设置的时区和时间显示的解决方案:

void doMinutes(NSUInteger minutes) {
    static const NSUInteger minutesInDay = 60 * 24;

    if (minutes >= minutesInDay) {
        minutes = minutes % minutesInDay;
    }

    //Create a new date at midnight GMT
    NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:minutes * 60];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setTimeStyle:NSDateFormatterShortStyle];
    [fmt setDateStyle:NSDateFormatterNoStyle];
    [fmt setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSLog(@"minutes: %ld, new date: %@", minutes, [fmt stringFromDate:newDate]);
    [fmt release];
    [newDate release];
}

int main(int argc, char *argv[]) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

    doMinutes(480);
    doMinutes(765);
    doMinutes(840);
    doMinutes(30);
    doMinutes(1430);
    [p release];
}

日志输出:

minutes: 480, new date: 8:00 AM
minutes: 765, new date: 12:45 PM
minutes: 840, new date: 2:00 PM
minutes: 30, new date: 12:30 AM
minutes: 1430, new date: 11:50 PM

将语言环境设置为英国时的日志输出:

minutes: 480, new date: 08:00
minutes: 765, new date: 12:45
minutes: 840, new date: 14:00
minutes: 30, new date: 00:30
minutes: 1430, new date: 23:50