繁体   English   中英

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

[英]Objective-C: Convert minutes to time

我将午夜后的分钟数转换为正确的时间时遇到问题。 直到我中午之前,它都可以正常工作。 此时,时间变为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的Edit 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;        

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;        

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM