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

标签 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,它运行良好,只是当我通过 dateFormatter 运行它时,它似乎从 newDate 中减去了 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

最佳答案

首先,也许您使用格式字符串的方法有点过于复杂。不要忘记,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;        

编辑2:

根据要求扩展上面的代码:

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;        

关于iphone - Objective-C:将分钟转换为时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011136/

相关文章:

ios - CoreData内存使用: differences between iOS 5 and iOS 4. 3?

ios - TeenSafe iOS 应用程序如何访问短信?

objective-c - 分析器关于引用计数错误减少的警告

ios - 从Unity部署到IOS模拟器出现这些Xcode提示/错误正常吗?

objective-c - 在没有 CoreTelephony 的情况下获取 iPhone 运营商

ios - 我怎样才能用 crittercism 解决 SIGSEGV

ios5 - 文件不支持ARC功能,如何处理

iphone - 如何使用swift在游戏中心报告高分

ios - XCode iPhone 模拟器看起来不像 iPhone

ios - objective c 如何为整个应用程序的所有 UI 按钮设置独占触摸