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;

    }

  • 相关阅读:
    MySQL支持的数据类型(2)( 日期)
    MySQL支持的数据类型(1)( 整数,小数,位)
    MySQL查询语句(select)详解(2)
    MySQL查询语句(select)详解(1)
    MySQL sql_safe_updates 分析
    MySQL中INFORMATION_SCHEMA是什么?(2)
    js event事件对象概括
    js String字符串对象常见方法总结
    js Array数组对象常见方法总结
    纯js轮播图
  • 原文地址:https://www.cnblogs.com/cui-cui/p/6140152.html
Copyright © 2011-2022 走看看