iOS 计算时间差   避免过度使用定时器_篱下悠然_新浪博客

代码繁琐,先做笔记,有时间进行优化

 NSString *recordDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"recordDate"];

    

    if (recordDate.length != 0) {

    

        NSDate *nowDate = [NSDate date]; // 当前时间

    

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

        

        [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

        

        NSString*timeString=[formatter stringFromDate: nowDate];

      

        double timeDiff = 0.0;

        

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

        [formatters setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

        NSDate *dateS = [formatters dateFromString:recordDate];

        

        

        

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

        [formatterE setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

        NSDate *dateE = [formatterE dateFromString:timeString];

        

        

        timeDiff = [dateE timeIntervalSinceDate:dateS ];

        int myInt = (int)timeDiff;

        if (myInt < 20) {

           

            MBProgressHUD *hud = [[MBProgressHUD alloc] init];

            [self.view addSubview:hud];

            hud.labelText = @"请勿频繁操作";//@"网络连接失败";

            hud.mode = MBProgressHUDModeText;

            [hud showAnimated:YES whileExecutingBlock:^{

                sleep(1);

            } completionBlock:^{

                [hud removeFromSuperview];

            }];

            return;

           

        }

  

    }




#pragma mark-保存本地时间到沙盒

-(void)currentDateString{

    

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

    

    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

    

    NSString*timeString=[formatter stringFromDate: [NSDate date]];

    

//    NSDate *currentDate = [NSDate date];

//    NSString *currentDateString = [NSString stringWithFormat:@"%ld", (long)[currentDate timeIntervalSince1970]];

    [[NSUserDefaults standardUserDefaults] setObject:timeString forKey:@"recordDate"];

    [[NSUserDefaults standardUserDefaults] synchronize];

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值