zoukankan      html  css  js  c++  java
  • 时间差计算

    //是不是过了指定的天数
    - (BOOL) isAfterDays:(int) days {
    
        NSDate * sendDate = [NSDate date];
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString * locationString = [dateFormatter stringFromDate:sendDate];
        
        NSString * lastShowTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastShowTime"];
        if (lastShowTime == nil) {
            [[NSUserDefaults standardUserDefaults] setObject:locationString forKey:@"lastShowTime"];
        }
        else {
            NSString * string = [self intervalSinceNow:lastShowTime];
            int timeInterval = [string intValue];
            
            NSLog(@"Interval:%@",string);
            if (timeInterval >= days) {
                [self saveLastShowTime];
                return YES;
            }
        }
        
        return NO;
    }
    
    //存储本次提醒时间,以便下次计算下次提醒的时间
    - (void) saveLastShowTime {
        NSDate * sendDate = [NSDate date];
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString * locationString = [dateFormatter stringFromDate:sendDate];
        [[NSUserDefaults standardUserDefaults] setObject:locationString forKey:@"lastShowTime"];
    }
    
    #pragma mark 获取指定日期距离现在的时间段
    - (NSString *)intervalSinceNow: (NSString *) theDate
    {
        
        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 dateWithTimeIntervalSinceNow:0];
        NSTimeInterval now=[dat timeIntervalSince1970]*1;
        NSString *timeString=@"";
        
        NSTimeInterval cha=now-late;
        
        //**********
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@", timeString];
        //*********
        
        return timeString;
    }
  • 相关阅读:
    bug篇——generator逆向出现配置文件不存在
    安装篇——Linux下安装mysql
    安装篇——linux服务器安装jdk、mysql、nginx、fastdfs
    基础篇——浅谈Base64
    基础篇—AOP
    基础篇—List、Set、Map
    工具类篇——I/O读取文件
    基础篇——Spring之XML配置Bean的属性注入
    简单了解malloc分配内存
    通过指针形参修改实参的值2
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3937960.html
Copyright © 2011-2022 走看看