美文网首页
Unix时间戳转化

Unix时间戳转化

作者: 冲上云霄90 | 来源:发表于2016-08-13 18:24 被阅读28次
    + (NSString *)getUnixTimeString:(NSString *)timeStr{
        double unixTimeStamp = [timeStr doubleValue];
        NSTimeInterval _interval=unixTimeStamp;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
        [_formatter setLocale:[NSLocale currentLocale]];
        [_formatter setDateFormat:@"yyyy.MM.dd kk.mm"];
        NSString * _date=[_formatter stringFromDate:date];
        return _date;
    }
    
    + (NSString *)getUnixTimeInt:(NSInteger)timeInt {
    
        NSTimeInterval _interval= (double)timeInt;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
        [_formatter setLocale:[NSLocale currentLocale]];
        [_formatter setDateFormat:@"yyyy.MM.dd kk.mm"];
        NSString * _date=[_formatter stringFromDate:date];
        return _date;
    }
    
    //返回上下午 返回12小时制时间
    
    +(NSDictionary *) changeSptoTwelveTime:(NSString *)spString
    {
        NSDate * timeDate = [self changeSpToTime:spString];
        NSString * str1 = [self nsdateToString:timeDate];
        NSString * str2 = [str1 substringWithRange:NSMakeRange(11,5)];
        NSInteger str3 = [[str2 substringWithRange:NSMakeRange(0,2)] integerValue];
        
        NSString * state = nil;
        NSString * time = nil;
        
        if (str3 >= 12) {
            state = @"PM";
        } else{
            state = @"AM";
        }
        
        NSString * str4 = [self nsdateToStrings:timeDate];
        NSString * str5 = [str4 substringWithRange:NSMakeRange(11,5)];
        time = str5;
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:state,@"state",time,@"time",nil];
        
        return dic;
    }
    
    ///这个方法针对固定格式的时间需求 例如只需要HH:mm 那么传入时间戳以及你所需要的那部分在字符串中所占的位置
    +(NSString *)changeSpToTime:(NSString*)spString Start:(NSInteger) start Length:(NSInteger)length//将时间戳转换成NSString并截取相应长度
    {
        NSDate * timeDate = [self changeSpToTime:spString];
        NSString * str1 = [self nsdateToString:timeDate];
        NSString * str2 = [str1 substringWithRange:NSMakeRange(start, length)];
        
        return str2;
    }
    
    //获取当前系统的时间戳
    +(long)getTimeSp{
        
        long time;
        NSDate *fromdate=[NSDate date];
        time=(long)[fromdate timeIntervalSince1970];
        
        return time;
    }
    
    //将时间戳转换成NSDate
    +(NSDate *)changeSpToTime:(NSString*)spString{
        
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
        
        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];
        
        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];
        
        return string;
    }
    
    //将NSDate按yyyy-MM-dd hh:mm:ss格式时间输出
    +(NSString*)nsdateToStrings:(NSDate *)date{
        
        NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSString* string=[dateFormat stringFromDate:date];
        
        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];
        
        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;
    }
    

    相关文章

      网友评论

          本文标题:Unix时间戳转化

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