ios 时间转换

#pragma mark - 将某个时间转化成 时间戳

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];         

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:format]; 

//(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];


    [formatter setTimeZone:timeZone];

    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate

    //时间转时间戳的方法:

    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];

    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值

    return timeSp;


}

//时间戳转成时间

//字符串转为时间,时间格式自己定

+(NSString *)timestampSwitchTime:(NSString *)time{

//    NSString * time = @"594947489";//时间字符串

    NSInteger num = [time integerValue];//转为int型

    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];//时间管理

    [formatter setDateStyle:NSDateFormatterMediumStyle];//时间方式

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"YYYY-MM-dd"];

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

    [formatter setTimeZone:timeZone];

    NSDate * confromTime = [NSDate dateWithTimeIntervalSince1970:num];

    NSString * comfromTimeStr = [formatter stringFromDate:confromTime];

    NSLog(@"%@",comfromTimeStr);

    return comfromTimeStr;

}

//GMT只需要将代码中的UTC替换为GMT即可

//将本地日期字符串转为UTC日期字符串

//本地日期格式:2013-08-03 12:53:51

//可自行指定输入输出格式

+ (NSString *)getUTCFormateLocalDate:(NSString *)localDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    //输入格式

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *dateFormatted = [dateFormatter dateFromString:localDate];NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone];

    //输出格式

    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];

    return dateString;

}

//将UTC日期字符串转为本地时间字符串

//输入的UTC日期格式2013-08-03T04:53:51+0000

+ (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    //输入格式

    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; [dateFormatter setTimeZone:localTimeZone];

    NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];

    //输出格式

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];

    return dateString;

}

//将GMT日期字符串转为本地时间字符串

//输入的GMT日期格式Tue May 31 17:46:55 +0800 2011

+ (NSString *)getLocalDateFormateGMTDate:(NSString *)gmtDate{

    // 时间字符串

//    NSString *string = @"Tue May 31 17:46:55 +0800 2011";

    // 日期格式化类

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";

    //fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy";

    // 设置语言区域

    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; [dateFormatter setTimeZone:localTimeZone];

    NSDate *dateFormatted = [dateFormatter dateFromString:gmtDate];


    //输出格式

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];

    return dateString;

}

//方式二 后台给的格式为 纯数字1352170595000(13位)

+(NSString *)updateTimeForRow:(NSString *)str {

    // 获取当前时时间戳 1466386762.345715 十位整数 6位小数

    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];

    // 创建歌曲时间戳(后台返回的时间 一般是13位数字)

    NSTimeInterval createTime =[str floatValue]/1000;

    // 时间差

    NSTimeInterval time = currentTime - createTime;


    //秒转分钟

    NSInteger small = time / 60;

    if (small == 0) {

        return [NSString stringWithFormat:@"刚刚"];

    }


    if (small < 60) {

        return [NSString stringWithFormat:@"%ld分钟前",small];

    }


    // 秒转小时

    NSInteger hours = time/3600;

    if (hours<24) {

        return [NSString stringWithFormat:@"%ld小时前",hours];

    }

    //秒转天数

    NSInteger days = time/3600/24;

    if (days < 30) {

        return [NSString stringWithFormat:@"%ld天前",days];

    }

    //秒转月

    NSInteger months = time/3600/24/30;

    if (months < 12) {

        return [NSString stringWithFormat:@"%ld月前",months];

    }

    //秒转年

    NSInteger years = time/3600/24/30/12;

    return [NSString stringWithFormat:@"%ld年前",years];

}

//方式一 后台给的格式为yyyy-MM-dd HH:mm:ss.SSS

+(NSString *) compareCurrentTime:(NSString *)str

{

    //把字符串转为NSdate

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *timeDate = [dateFormatter dateFromString:str];

    NSDate *currentDate = [NSDate date];


    //得到两个时间差

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];

    long temp = 0;

    NSString *result;

    if (timeInterval/60 < 1)

    {

        result = [NSString stringWithFormat:@"刚刚"];

    }

    else if((temp = timeInterval/60) <60){

        result = [NSString stringWithFormat:@"%ld分钟前",temp];

    }

    else if((temp = temp/60) <24){

        result = [NSString stringWithFormat:@"%ld小时前",temp];

    }

    else if((temp = temp/24) <30){

        result = [NSString stringWithFormat:@"%ld天前",temp];

    }

    else if((temp = temp/30) <12){

        result = [NSString stringWithFormat:@"%ld月前",temp];

    } else{

        temp = temp/12;

        result = [NSString stringWithFormat:@"%ld年前",temp];

    }

    return  result;

}

原文链接:https://blog.csdn.net/dkq972958298/article/details/77931453

你可能感兴趣的:(ios 时间转换)