时间差计算

2017-03-02  本文已影响26人  PZcoder

计算选择时间与当前时间的时间差:

整体思路
1、首先判断输入日期格式是否正确:年月日时分秒在合理范围内、闰年及平年二月天数问题。
2、比较秒单位,60秒以内输出秒;其次比较分,60分以内输出分;输出较大单位数值,忽略小单位数值。如12时30分,只输出“12小时前”。

时间差计算coding地址(包括启动图、datepicker使用)

主要代码如下:

/*
 调用方法
 Timestamp *timestamp = [Timestamp new];
 NSString *dateStr = [timestamp timeStampWith:ddd[@"publishDate"]];
 */
- (NSString *)timeStampWith:(NSString *)otherStringOfTime
{
    NSString * str = [NSString stringWithString:otherStringOfTime];
    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * date = [NSDate date];
    
    NSArray * arrFir = [str componentsSeparatedByString:@" " ];
    NSArray * arrSec = [arrFir[0] componentsSeparatedByString:@"-"];//数组里是yy,MM,dd
    NSNumber * yy = (NSNumber *)arrSec[0];
    NSNumber * MM = (NSNumber *)arrSec[1];
    NSNumber * dd = (NSNumber *)arrSec[2];
    
    NSArray * arrThi = [arrFir[1] componentsSeparatedByString:@":"];//数组里是hh,mm,ss
    NSNumber * hh = (NSNumber *)arrThi[0];
    NSNumber * mm = (NSNumber *)arrThi[1];
    NSNumber * ss = (NSNumber *)arrThi[2];

    if ((yy.intValue%1==0)&&
        (MM.intValue>=1)&&(MM.intValue<=12)&&
        (dd.intValue>=1)&&(dd.intValue<=31)&&
        (hh.intValue>=0)&&(hh.intValue<=23)&&
        (mm.intValue>=0)&&(mm.intValue<=59)&&
        (ss.intValue>=0)&&(ss.intValue<=59))//判断年月日时分秒是否符合条件
    {
        int febday = 0;//存放二月的天数
        
        if ((yy.intValue%4==0&&yy.intValue%100!=0)||(yy.intValue%400==0))//判断闰年情况MM=29
        {
            febday = 29;
        }
        else
        {
            febday = 28;
        }
        
        NSNumber * feb = [NSNumber numberWithInt:febday];
        NSArray * array = @[@31,feb,@30,@30,@31,@30,@31,@31,@30,@31,@30,@31];
        
        if ([self isAllNum:arrSec[0]]==YES&&[self isAllNum:arrSec[1]]==YES&&
            [self isAllNum:arrSec[2]]==YES&&[self isAllNum:arrThi[0]]==YES&&
            [self isAllNum:arrThi[1]]==YES&&[self isAllNum:arrThi[2]]==YES)
        {
            switch (MM.intValue)
            {
                case 4:case 6:case 9:case 11:
                    if (dd.intValue >30)
                    {
                        return @"dd输入错误!";
                    }
                    break;
                    
                case 2:
                    if (dd.intValue >febday)
                    {
                        return @"dd输入错误!";
                    }
                    break;
                    
                default:
                    break;
            }
            
            //对秒进行比较
            date = [formatter dateFromString:str];
            NSTimeInterval time = fabs([date timeIntervalSinceNow]);//秒

            if (time<60)
            {
                return [NSString stringWithFormat:@"%d秒前",(int)time];
            }
            else if (time>=60&&time/60<60)
            {
                return [NSString stringWithFormat:@"%d分钟前",(int)time/60];
            }
            else if (time/60>=60&&time/60/60<24)
            {
                return [NSString stringWithFormat:@"%d小时前",(int)time/60/60];
            }
            else if (time/60/60/24>=1&&time/60/60/24<((NSNumber *)array[MM.intValue-1]).doubleValue)
            {
                return [NSString stringWithFormat:@"%d天前",(int)time/60/60/24];
            }
            else if ((time/60/60/24>=((NSNumber *)array[MM.intValue-1]).intValue)&&
                     (time/60/60/24<[self yyDaywithMonth:MM.intValue andYear:yy.intValue]))
            {
                int month = 0;
                int n = MM.intValue;
                int day = (int )time/60/60/24;
                int MMsum = ((NSNumber *)array[MM.intValue-1]).intValue;
                
                while (day>=MMsum)
                {
                    month ++;
                    n ++;
                    MMsum += ([self MMnext:n witharray:array]).intValue;
                }
                
                return [NSString stringWithFormat:@"%d月前",month];
            }
            else
            {
                int year = 0;
                int n = yy.intValue;
                int day = (int )time/60/60/24;
                int yySum = [self yyDaywithMonth:MM.intValue andYear:yy.intValue];
                
                while (day>=yySum)
                {
                    year ++;
                    n ++;
                    yySum += [self yyNextYearDay:n];
                }
                
                return [NSString stringWithFormat:@"%d年前",year];
            }
        }
        else
        {
            return @"日期或时间输入错误!";
        }
    }
    else
    {
        return @"日期或时间输入错误!";
    }
}

/**
 判断是否都是数字
 */
- (BOOL)isAllNum:(NSString *)string
{
    unichar c;
    
    for (int i=0; i<string.length; i++)
    {
        c = [string characterAtIndex:i];
        
        if (!isdigit(c))
        {
            return NO;
        }
    }
    
    return YES;
}

/**
 计算MM月份的下一个月的天数
 */
-(NSNumber *)MMnext:(int)MM witharray:(NSArray *)arr
{
    if (MM<11)
    {
        return arr[MM+1];
    }
    else
    {
        return arr[0];
    }
}

/**
 计算yy下一年的天数
 */
-(int)yyNextYearDay:(int)yy
{
     if (((yy+1)%4==0&&(yy+1)%100!=0)||((yy+1)%400==0))
     {
         return 366;
     }
     else
     {
         return 365;
     }
}

/**
 计算输入日期到下一年此日期之间的天数
 */
-(int)yyDaywithMonth:(int)MM andYear:(int)yy
{
    if (MM > 2)
    {
        return [self yyNextYearDay:yy];
    }
    else
    {
        return [self yyNextYearDay:yy-1];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读