首页 > 其他 > 详细

时间差计算

时间:2014-08-26 19:39:28      阅读:417      评论:0      收藏:0      [点我收藏+]

 

//是不是过了指定的天数
- (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;
}

 

时间差计算

原文:http://www.cnblogs.com/benbenzhu/p/3937960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!