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

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

现整理时间相关点:

 //从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];
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 文/小昭11 望着昏黄的城市灯光,仿若我人生的彷徨,充满迷茫! 看完了季羡林老先生的《我的人生感悟》,文中关于季老...
    小昭11阅读 644评论 0 13
  • 有很多朋友经常这样问我:“你是女人,为何如此拼搏?” 我的答案:“因为我喜欢自己劳动,喜欢用自己的双手创造自己的想...
    擎晨马春燕阅读 197评论 0 0
  • 打一把红色的太阳伞,却没也想到是遮雨。乘一辆名为15路的公车,看一场只有自己的电影,逛一没有目的的超市,买半只母亲...
    丽丽嚒阅读 118评论 0 0
  • 深夜,摇曳着纤细的高脚杯,看杯中血样鲜红的酒,吟着“葡萄美酒夜光杯,欲饮琵琶马上催。”流泪了。 多么美的夜光杯啊,...
    梅园遗珠阅读 262评论 0 2