【问题标题】:Converting float into time将浮点数转换为时间
【发布时间】:2012-09-05 10:39:55
【问题描述】:

我有一个字符串值 20.30(hours) 我需要它来转换 08:30:00 。 有没有人知道的请帮帮我。

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    dateFormat.dateFormat = @"hh:mm:ss";
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

    double time = 20.30;
    double timeInSeconds = time*24*60*60; // 0.27392 * 24 = 6.57408 hours *60 for minutes * 60 for seconds

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInSeconds]; //Creates a date: 1 January 2001 6:34:27 AM, GMT

    NSLog(@"Time: %@", [dateFormat stringFromDate:date]);
    [dateFormat release];

上面的代码对我不起作用。

【问题讨论】:

  • 我认为您需要做的是创建一个格式为 @"hh.mm" 的日期格式化程序以匹配您的字符串。然后使用这个格式化程序从字符串创建一个 NSDate 对象。然后,您需要另一个输出格式为 @"hh:mm:ss" 的格式化程序,然后使用它来将您的 NSDate 转换为字符串!希望这会有所帮助,
  • 也只是一个注释,不太确定你想用 timeInSeconds 实现什么?因为 20:30 不是从现在开始的 20.30*24*3600 秒,除非您在拨打电话时非常幸运。
  • 同意@GeorgeGreen 的第一条评论.. 看看我的回答
  • 在没有提供正当理由的情况下投反对票是不好的......
  • @Melbourne 我没有投反对票。我说在没有提供适当理由的情况下投反对票不好...

标签: iphone objective-c ipad ios5


【解决方案1】:

用这个替换你的代码:

//The Format which you want as output
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = @"hh:mm:ss";
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

//The Format in which your dateTime currently is
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.dateFormat = @"HH.mm";
[dateFormat1 setTimeZone:[NSTimeZone systemTimeZone]];

double time = 20.30;

NSString *timeStr = [NSString stringWithFormat:@"%.2f",time];
NSDate *dates = [dateFormat1 dateFromString:timeStr];
NSLog(@"Time: %@", [dateFormat stringFromDate:dates]);

【讨论】:

  • 这不会正确转换时间。 20.5 小时是 20:50:00,这是错误的。它应该出现在 20:30:00
  • @Izac,我感谢您的贡献,但是如果您仔细查看问题第一行说 "字符串值 20.30(小时) 我需要它来转换 08:30: 00" 间接表示 20.5 = 8:50:00 而不是 8:30:00。此外,您已将 hh 更改为 HH,说明它是错误的。我想让你知道,小号hh 是 12 小时格式,大写 HH 是 24 小时格式。而且由于在任务中他已经指定他需要将 20.30 转换为 8:30:00,这再次意味着他想要 12 小时格式。所以我又是正确的。因此,根据问题,我在所有方面都是正确的
  • hmmm...不知道“HH:和hh”的东西...而且它很好学。我只是把这个问题理解错了。谢谢,我很抱歉,伙计..
【解决方案2】:

试试这是否可行:

[dateFormatter setDateFormat:@"HH:mm 'on' dd MMMM YYYY"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];  

【讨论】:

    【解决方案3】:

    您用来计算秒数的公式是错误的。

    你可以使用类似的东西

    double t = 20.30;
    
    int hours = (int) floor(t);
    int minutes = (int) ((t-hours)*100);
    
    int seconds = hours*60*60 + minutes*60;
    
    NSLog(@"%@",[NSDate dateWithTimeIntervalSinceNow:seconds]);
    

    【讨论】:

      【解决方案4】:

      试试这个:

      double time = 20.30;
      NSString *strTime = [NSString stringWithFormat:@"%.2f",time];
      NSArray *values = [strTime componentsSeparatedByString:@"."];
      
      double hours = [values objectAtIndex:0];
      double min = [values objectAtIndex:1];
      
      NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(hours*60*60)+mins*60]; 
      NSLog(@"Time: %@", [dateFormat stringFromDate:date]);
      

      【讨论】:

        【解决方案5】:

        试试这个

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        dateFormat.dateFormat = @"HH:mm";
        [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
        
        double time = 20.30;
        NSString* timeString=[NSString stringWithString:[[NSString stringWithFormat:@"%.2f",time]stringByReplacingOccurrencesOfString:@"." withString:@":"]];
        
        NSDate *date = [dateFormat dateFromString:timeString];
        dateFormat.dateFormat=@"hh:mm:ss";
        NSLog(@"Time: %@", [dateFormat stringFromDate:date]);
        

        这会将 20.30 转换为 08:30:00

        【讨论】:

          猜你喜欢
          • 2018-12-23
          • 1970-01-01
          • 1970-01-01
          • 2018-05-03
          • 2013-11-11
          • 2011-12-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多