• iOS


    • 在给定时间与当前时间进行对比,用途: 验证码是否过期?

    • 获取当前时间NSDate

    #pragma mark -得到当前时间date
    - (NSDate *)getCurrentTime{
        
        //2017-04-24 08:57:29
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString *dateTime=[formatter stringFromDate:[NSDate date]];
        NSDate *date = [formatter dateFromString:dateTime];
    //    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    //    NSString *dateString = [formatter stringFromDate:date];
    //    NSLog(@"datastring  = %@",dateString);
        return date;
    }
    
    • 将服务器返回的时间字符串转换为date格式
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    //下面以 '2017-04-24 08:57:29'为例代表服务器返回的时间字符串
        NSDate *date = [dateFormatter dateFromString:@"2017-04-24 08:57:29"];
    
    • 时间对比:
    • 返回1 - 过期, 0 - 相等, -1 - 没过期
    - (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
        NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
        NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
        NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
        NSComparisonResult result = [dateA compare:dateB];
        NSLog(@"oneDay : %@, anotherDay : %@", oneDay, anotherDay);
        if (result == NSOrderedDescending) {
            //在指定时间前面 过了指定时间 过期
            NSLog(@"oneDay  is in the future");
            return 1;
        }
        else if (result == NSOrderedAscending){
            //没过指定时间 没过期
            //NSLog(@"Date1 is in the past");
            return -1;
        }
        //刚好时间一样.
        //NSLog(@"Both dates are the same");
        return 0;
        
    }
    
    

    常用语验证码过期.封装到公共类最好.

    /*=============================NSDate日期类的使用=============================*/
        
        // 获取当前时间,获得的时0市区的时间跟北京时间相差8小时
        NSDate *currentDate = [NSDate date]; // GMT
        NSLog(@"currentDate :%@",currentDate);
        
        //timeIntervalSince1970 到1970-1-1的秒数,也叫时间戳(NSTimeInterval)
        NSTimeInterval interval1970 = [currentDate timeIntervalSince1970];
        NSLog(@"interval = %lf",interval1970);
        
        // timeIntervalSinceReferenceDate 到2001-1-1 的秒数
        NSTimeInterval interval2001 = [currentDate timeIntervalSinceReferenceDate];
        NSLog(@"interval2001 = %lf",interval2001);
        
        // timeIntervalSinceNow 距当前时间的秒数
        NSTimeInterval intervalNow = [currentDate timeIntervalSinceNow];
        NSLog(@"intervalNow = %lf",intervalNow);
        
        
        
        NSTimeInterval hour = 60 * 60;
        // 一小时后
        NSDate *h1 = [currentDate dateByAddingTimeInterval:hour];
        NSLog(@"h1 :%@",h1);
        
        // 一小时前
        NSDate *h2 = [currentDate dateByAddingTimeInterval:-hour];
        NSLog(@"h2 :%@",h2);
        
        // 计算北京时区时间,(使用系统当前的时区:systemTimeZone)
        NSTimeInterval inter = [[NSTimeZone systemTimeZone] secondsFromGMT];
        NSDate *bjDate = [currentDate dateByAddingTimeInterval:inter];
        NSLog(@"bjDate :%@",bjDate);
        
        
        
        NSTimeInterval day = 24 * 60 * 60;
        // 方式一:
        //        NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-day];
        // 方式二:
        
        // 昨天
        NSDate *yesterday = [bjDate dateByAddingTimeInterval:-day];
        NSLog(@"yesterday:%@",yesterday);
        
        // 今天
        NSDate *today = [bjDate dateByAddingTimeInterval:0];
        NSLog(@"today:%@",today);
        
        // 明天
        NSDate *tomorrow = [bjDate dateByAddingTimeInterval:day];
        NSLog(@"tomorrow:%@",tomorrow);
        
        // isEqualToDate 两个日期的比较
        if ([yesterday isEqualToDate:tomorrow]) {
            
            NSLog(@"两个日期相同");
            
        } else {
            
            NSLog(@"两个日期不相同");
            
        }
        
        // compare 两个日期的比较
        NSComparisonResult result = [yesterday compare:tomorrow];
        if (result == NSOrderedAscending) {
            
            NSLog(@"日期升序");
            
        } else if(result == NSOrderedSame) {
            
            NSLog(@"两个日期相同");
            
        } else if(result == NSOrderedDescending) {
            
            NSLog(@"两个日期降序");
        }
        
        //distantFuture 未来的一个时间 4001-01-01 00:00:00
        NSDate *future = [NSDate distantFuture];
        NSLog(@"future :%@",future);
        //distantPast 远古的一个时间 0001-12-30 00:00:00
        NSDate *past = [NSDate distantPast];
        NSLog(@"past :%@",past);
        
        
        // dateWithTimeIntervalSince1970 将时间戳转为日期类型
        NSString *time = @"34567898";
        NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
        NSLog(@"timeDate :%@",timeDate);
        
        NSDate *now = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
        
        // 2014年10月15日 16:35:42
        // stringFromDate 将日期类型格式化,转为NSString 类型
        NSString *current = [formatter stringFromDate:now];
        NSLog(@"current:%@",current);
        
        //dateFromString 将时间字符串转化为日期类型,
        [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // 默认使用0时区,所以需要时区的转换
        NSDate *nowDate = [formatter dateFromString:current];
        NSLog(@"nowDate :%@",nowDate);
    
    
    
    • 获取时间戳
    -(NSString*)getCurrentTimestamp{
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a=[dat timeIntervalSince1970];
        NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
        return timeString;
    }
    
  • 相关阅读:
    php原生PHPExcel插件导表(附表格合并,加粗居中及加边框换行操作)
    VMware虚拟机基于contos 7 搭建lnmp环境全过程
    菜鸟学git的基本命令及常见错误
    为什么要写blog????
    JavaScript高级程序设计--函数小记
    jsapi微信支付
    Javascript模式小记(一)
    图片的增删查
    图形的滚动及颜色区域的拉移
    网页中的宽与高
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6755263.html
Copyright © 2020-2023  润新知