zoukankan      html  css  js  c++  java
  • 计算一个时间距离现在的时间差的方法

    计算一个时间字符串距离现在的时间间隔

    #pragma mark-一个时间距现在的时间

    - (NSString *)intervalSinceNow: (NSString *) theDate

    {

        NSArray * timeArray;

        if ([theDate rangeOfString:@"T"].location != NSNotFound)

        {

            timeArray=[theDate componentsSeparatedByString:@"T"];

            theDate=[NSString stringWithFormat:@"%@ %@",[timeArray objectAtIndex:0],[timeArray objectAtIndex:1]];

        }

        

        NSDateFormatter *date=[[NSDateFormatter alloc] init];

        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        NSDate *d=[date dateFromString:theDate];

        

        NSTimeInterval late=[d timeIntervalSince1970]*1;

        

        

        NSDate* dat = [NSDate date];

        

        NSTimeInterval now=[dat timeIntervalSince1970]*1;

        

        NSTimeInterval cha=late-now;

        

        NSString *timeString=@"";

        NSString * dayStr = @"";

        NSString * hourStr = @"";

        NSString * minStr = @"";

        if (cha <=0)

        {

            timeString = @"已结束";

        }

        

        if (cha/3600/24 > 1)

        {

            dayStr = [NSString stringWithFormat:@"%d", (int)cha/3600/24];

            timeString=[NSString stringWithFormat:@"%@天", dayStr];

            cha = cha-3600*24*[dayStr intValue];

        }

        

        

        if (cha/3600 > 1) {

            hourStr = [NSString stringWithFormat:@"%d", (int)cha/3600];

            timeString=[NSString stringWithFormat:@"%@%@小时", timeString,hourStr];

            cha = cha-3600*[hourStr intValue];

        }

        

        if (cha/60 > 1) {

            minStr = [NSString stringWithFormat:@"%d", (int)cha/60];

            timeString=[NSString stringWithFormat:@"%@%@分", timeString,minStr];

            cha = cha-60*[minStr intValue];

        }

        

        if (cha > 1) {

            NSString * secondStr = [NSString stringWithFormat:@"%d", (int)cha];

            timeString=[NSString stringWithFormat:@"%@%@秒", timeString,secondStr];

        }

        

        return timeString;

    }

  • 相关阅读:
    LRU
    c++ 在临时变量上使用const引用
    剑指 Offer 13. 机器人的运动范围
    C++之对象包含与成员函数不兼容的类型限定符
    C#獲取指定格式日期
    在ORACLE產生001,002的流水號
    ASP.NET中DataList数字分页代码
    生成條碼標的Class
    sql 将横的记录显示为竖的记录 max(case when CASE ltrim(ps.SIZE) WHEN '4.5' THEN ps.PairPerCarton END is null then null else ps.PairPerCarton end ) AS [4.5]
    为什么margin-top值不是作用域父元素
  • 原文地址:https://www.cnblogs.com/cui-cui/p/6140152.html
Copyright © 2011-2022 走看看