美文网首页iOS项目框架搭建iOS Developer基础类
iOS时间转换相关整理(附送试用期逻辑处理)

iOS时间转换相关整理(附送试用期逻辑处理)

作者: smile丽语 | 来源:发表于2016-07-06 14:38 被阅读468次

    对于时间转换我们并不陌生,只是不同的后台风格不同,我们请求回来的时间格式也不同!

    现整理时间相关点:

     //从1970年开始到现在经过了多少秒
     -(NSString *)getTimeSp
     {
     NSString *time;
     NSDate *fromdate=[NSDate date];
     time = [NSString stringWithFormat:@"%f",[fromdate timeIntervalSince1970]];
     return time;
     }
    
     //将时间戳转换成NSDate,转换的时间我也不知道是哪国时间,应该是格林尼治时间
     -(NSDate *)changeSpToTime:(NSString*)spString
     {
     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
     NSLog(@"%@",confromTimesp);
     return confromTimesp;
     }
    
     //将时间戳转换成NSDate,加上时区偏移。这个转换之后是北京时间
     -(NSDate*)zoneChange:(NSString*)spString
     {
     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
     NSTimeZone *zone = [NSTimeZone systemTimeZone];
     NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
     NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
     NSLog(@"%@",localeDate);
     return localeDate;
     }
    
     //比较给定NSDate与当前时间的时间差,返回相差的秒数
     -(long)timeDifference:(NSDate *)date
     {
     NSDate *localeDate = [NSDate date];
     long difference =fabs([localeDate timeIntervalSinceDate:date]);
     return difference;
     }
    
     //将NSDate按yyyy-MM-dd HH:mm:ss格式时间输出
     -(NSString*)nsdateToString:(NSDate *)date
     {
     NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
     [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSString* string=[dateFormat stringFromDate:date];
     NSLog(@"%@",string);
     return string;
     }
    
     //将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳
     -(long)changeTimeToTimeSp:(NSString *)timeStr
     {
     long time;
     NSDateFormatter *format=[[NSDateFormatter alloc] init];
     [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSDate *fromdate=[format dateFromString:timeStr];
     time= (long)[fromdate timeIntervalSince1970];
     NSLog(@"%ld",time);
     return time;
     }
    
     //获取当前系统的yyyy-MM-dd HH:mm:ss格式时间
     -(NSString *)getTime
     {
     NSDate *fromdate=[NSDate date];
     NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
     [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSString* string=[dateFormat stringFromDate:fromdate];
     return string;
     }
    
     //将当前时间转化为年月日格式
     -(NSString *)changeDate
     {
     NSDate *date = [NSDate date];
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
     NSInteger unitFlags =  NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
     NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
     
     NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
     NSInteger year = [comps year];
     NSInteger month = [comps month];
     NSInteger day = [comps day];
     NSInteger hour = [comps hour];
     NSInteger min = [comps minute];
     NSInteger sec = [comps second];
     
     NSString *string = [NSString stringWithFormat:@"%d年%d月%d日%d时%d分%d秒",year,month,day,hour,min,sec];
     
     NSLog(@"%@",string);
     return string;
     }
    

    现将我自定义封装的时间转换如下:

    1.时间戳 转换 时间

    //  时间戳转换时间
    #import <Foundation/Foundation.h>
    
    @interface NSString (QTXTime)
    - (NSString *)timeChangeWithDateFormat:(NSString *)dateFormat;
    @end
    
    #import "NSString+QTXTime.h"
    
    @implementation NSString (QTXTime)
    
    // 时间戳转日期 此情况属于server并未有时区区域区分
    - (NSString *)timeChangeWithDateFormat:(NSString *)dateFormat {
        
        double publishLong = [self doubleValue];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [formatter setDateFormat:dateFormat];
        
        NSDate *publishDate = [NSDate dateWithTimeIntervalSince1970:publishLong / 1000]; // 这是毫秒转换成秒 如果本来server传来值就是秒没必要除1000了
        
        NSDate *date = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate:date];
        publishDate = [publishDate  dateByAddingTimeInterval:interval];
        
        NSString *str = [formatter stringFromDate:publishDate];
        
        return str;
    }
    @end
    

    在相对应的model里使用时转换,例如:

    // 订单时间
    @property (nonatomic, copy) NSString *orderTime;
    
    // 将时间戳转化为时间
    - (NSString *)orderTime {
        
        return [_orderTime timeChangeWithDateFormat:@"yyyy-MM-dd hh:mm"]; // 时间格式选择自己需要展示的格式
    }
    

    2. 时间字符串 转换 时间

    //  时间转化
    
    #import <Foundation/Foundation.h>
    
    @interface NSDate (QTXDateTimeStr)
    
    // 时间转字符串
    - (NSString *)timeFormat:(NSString *)dateFormat;
    
    // 字符串转时间
    + (NSDate *)stringChangeTimeFormat:(NSString *)dateFormat string:(NSString *)string;
    
    @end
    
    #import "NSDate+QTXDateTimeStr.h"
    
    @implementation NSDate (QTXDateTimeStr)
    
    - (NSString *)timeFormat:(NSString *)dateFormat {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [formatter setDateFormat:dateFormat];
        
        return [formatter stringFromDate:self];
    }
    
    + (NSDate *)stringChangeTimeFormat:(NSString *)dateFormat string:(NSString *)string {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [formatter setDateFormat:dateFormat];
        
        return [formatter dateFromString:string];
        
    }
    
    @end
    

    使用时,调用方法即可,举个栗子吧:

     // 显示当前的不同格式的展示时间
    NSDate *currentTime = [NSDate date];
    NSInteger day = [[currentTime timeFormat:@"dd"] integerValue];
    
    NSDate *currentTime = [NSDate date];
    NSString *currentTimeStr = [currentTime timeFormat:@"yyyy年MM月"];
    
    // 字符串转时间
     NSDate *date = [NSDate stringChangeTimeFormat:@"yyyy年MM月" string:currentTimeStr];
    

    3. 时间差处理

    像一些APP 会有试用期,这个时候我们需要取到本地时间与注册时间的时间差,看看有木有过期.然后根据判断做出相应的处理.
    举个🌰:
    1>取到本地时间与注册时间的时间差

    // 判断是否还在10天试用期内
    - (void)judgeForDeadline {
        
        NSString *createTimeStr = [XHHAccountTool account].createTime;
        NSString *createtime = [createTimeStr timeChangeWithDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        int addDays = 10;
        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
        NSDate *createDate = [dateFormatter dateFromString:createtime];
        NSDate *deadlineDate = [createDate dateByAddingTimeInterval:60 * 60 * 24 * addDays];
        
        // 注册时间
        NSTimeInterval start = [createDate timeIntervalSince1970]*1;
        // 截止时间
        NSTimeInterval end = [deadlineDate timeIntervalSince1970]*1;
        self.diffTime = end - start;
    }
    

    2> 当status:1是会员可用, 0是非会员时,但在试用期内可用

    if ([[XHHAccountTool account].status isEqualToString:@"1"] || ([[XHHAccountTool account].status isEqualToString:@"0"] && self.diffTime > 0)) { // 会员 // 还在10天试用期内
                
                XHHModifiedFormulaController *vc = [[XHHModifiedFormulaController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                
            } else {
                XHHAlterView *alter = [[XHHAlterView alloc] initWithTitle:@"提示" Message:@"您还没成为会员,暂时无法使用该功能, 请充值会员." Prompt:@"" delegate:self rightButtonTitle:@"确定" otherButtonTitles:nil];
                [alter show];
            }
    

    相关文章

      网友评论

      本文标题:iOS时间转换相关整理(附送试用期逻辑处理)

      本文链接:https://www.haomeiwen.com/subject/ihrkjttx.html