• iOS开发NSDate、NSString、时间戳之间的转化


    //将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间)

    //NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000 
    //转化后:2018-03-27 14:54:41 +0000 
    -(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    NSInteger seconds = [tz secondsFromGMTForDate: UTCDate];  
    return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate];
     }
    //将当地时区的标准Date转化为UTCDate 
    //当前当地的标准时间:2018-03-27 14:54:41 +0000 
    //转化为世界标准时间:2018-03-27 06:54:41 +0000 
    -(NSDate *)getUTCDateFromLocalDate:(NSDate *)LocalDate{
     NSTimeZone *tz = [NSTimeZone defaultTimeZone];
     NSInteger seconds = -[tz secondsFromGMTForDate: LocalDate];
     return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate];
     }
    //根据UTCDate获取当前时间字符串(钟表上显示的时间)
     //输入:[NSDate date]     2018-03-27 07:44:05 +0000
     //输出:2018-03-27 15:44:05  
    -(NSString *)localStringFromUTCDate:(NSDate *)UTCDate{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];  
    [dateFormatter setTimeZone:tz];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* result=[dateFormatter stringFromDate:UTCDate]; 
    return result; 
    }
    //根据UTC字符串获取当前时间字符串(钟表上显示的时间)
     //输入:2018-03-27 07:44:05
     //输出:2018-03-27 15:44:05
     -(NSString *)localStringFromUTCString:(NSString *)UTCString{ 
    //先将UTC字符串转为UTCDate; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
     [dateFormatter setTimeZone:tz]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *UTCDate = [dateFormatter dateFromString:UTCString];  
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSString* result = [dateFormatter stringFromDate:UTCDate];  
    return result;  }
    //将当前时间字符串转为UTCDate 
    -(NSDate *)UTCDateFromLocalString:(NSString *)localString{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:localString]; 
    return date;
    }
    //将当前时间字符串转为UTC字符串 
     -(NSString *)UTCStringFromLocalString:(NSString *)localString{
    NSDate *date = [self UTCDateFromLocalString:localString]; 
    NSString *string= [NSString stringWithFormat:@"%@",date]; 
    NSString *result = [stringsubstringToIndex:string.length-6]; 
    return result;  
    }
    //UTCDate转UTC字符串
    -(NSString *)UTCStringFromUTCDate:(NSDate *)UTCDate{ 
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init]; 
    [dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [dataFormatter setTimeZone:tz];
    NSString *UTCString = [dataFormatter stringFromDate:UTCDate]; 
    return UTCString;
     }
    //将当前时间(UTCDate)转为时间戳
     -(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{
     NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970]; 
    // *1000,是精确到毫秒;这里是精确到秒; 
    NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];
    return result; 
    }
    //当前时间字符串(钟表上显示的时间)转为时间戳 
    -(NSString *)timeStamapFromLocalString:(NSString *)localString{
    //先转为UTCDate 
    NSDate *UTCDate = [self UTCDateFromLocalString:localString];
    NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
    return timeStamap;
    }
    //将UTCString转为时间戳
    -(NSString *)timeStamapFromUTCString:(NSString *)UTCString{
    //先将UTC字符串转为UTCDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    [dateFormatter setTimeZone:tz];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *UTCDate = [dateFormatter dateFromString:UTCString];
    NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
    return timeStamap;
    }
    //时间戳转UTCDate
    -(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{
    NSTimeInterval timeInterval=[timeStamap doubleValue];
    //   1000; 传入的时间戳timeStamap如果是精确到毫秒的记得要/1000
    NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];
    return UTCDate;
    }
  • 相关阅读:
    接口测试基础一--HTTP请求
    python笔记8-python的异常处理
    web自动化测试中的八大定位方法,推荐使用xpath
    charles抓取https包
    Python 中WebElement 类中的部分操作
    selenium 启动浏览器后基本操作:后退、前进、刷新、关闭窗口、关闭会话
    fiddler抓包可以抓到电脑数据抓不到手机上的数据------防火墙问题
    Charles的安装与破解
    python+ selenium + webdriver的环境准备
    python——print函数
  • 原文地址:https://www.cnblogs.com/OIMM/p/10904120.html
Copyright © 2020-2023  润新知