【问题标题】:How to get accuracy days completed如何完成准确天数
【发布时间】:2018-04-09 14:14:03
【问题描述】:

如何完成准确天数?

- (CGFloat)accuracyDaysCompleted:(NSString *)startDate and:(NSString *)endDate {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
                                                        fromDate:[dateFormatter dateFromString:startDate]
                                                          toDate:[dateFormatter dateFromString:endDate]
                                                         options:0];
    return  (CGFloat)components.day;

}

示例

工作完成 3 天 12 小时 30Min 预期结果 3.55 天。

【问题讨论】:

    标签: ios objective-c nsdate nsdateformatter


    【解决方案1】:

    以下示例显示了给定日期与今天的天数差异。

    这可能会指导您将秒、分钟、小时转换为天。

    -(NSString*)timeDifferenceConversion:(NSString*)dateString
    {
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate* firstDate = [dateFormatter dateFromString:dateString];
        NSDate * now = [NSDate date];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *newDateString = [dateFormatter stringFromDate:now];
        NSDate* secondDate = [dateFormatter dateFromString:newDateString];
        NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
        NSInteger ti = (NSInteger)timeDifference;
        NSInteger minutes = (ti / 60) % 60;
        NSInteger hours = (ti / 3600);
        NSInteger days = (ti / 86400);
        NSString* temp=@"";
    
        if(minutes<=2 && hours<1 && days<1)
        {
            temp=@"now";
        }
        else if(minutes>2 && hours<1 && days<1)
        {
           temp=[NSString stringWithFormat:@"%ld minutes ago", (long)minutes];
        }
        else if(hours<=24 && days<1)
        {
            temp=[NSString stringWithFormat:@"%ld hours ago", (long)hours];
        }
        else if(days<=1)
        {
            temp=[NSString stringWithFormat:@"%ld day ago",(long)days];
        }
        else if(days>1)
        {
            temp=[NSString stringWithFormat:@"%ld days ago",(long)days];
        }
    
        return temp;
    
    }
    

    【讨论】:

    • 我们可以在此处包含以下条件的月份和年份吗?
    【解决方案2】:

    我建议你计算两个日期之间的秒数差,然后简单地将结果除以一天的秒数:

    - (CGFloat)accuracyDaysCompleted:(NSString *)startDate and:(NSString *)endDate {
        NSDate *date1 = [NSDate dateWithString: startDate];
        NSDate *date2 = [NSDate dateWithString: endDate];
    
        NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
        return secondsBetween / 86400.0f;
    }
    

    【讨论】:

    • 84000 不是很准确?
    • 在这种情况下,我正在等待您提供更好的解决方案,至少解释一下原因……我也是来学习的 ;-)
    • 首先 24 小时是 86400 秒。但是由于夏令时的变化,一天可能有 23、24 或 25 小时。
    • 感谢您的重播@vadian
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2015-11-30
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多