无标题文章

    #import "NSString+BBPadTimestamp.h"

    @implementation NSString (BBPadTimestamp)

    // 时间戳转化为时间字符串
            + (NSString *)transToStringWithTimestamp:(int64_t)timeStamp FormatStr:(NSString *)formatStr
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeZone:[NSTimeZone localTimeZone]];
        [formatter setDateFormat:formatStr];

        // 如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
        NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:timeStamp];
        NSString *currentDateStr = [formatter stringFromDate: detaildate];
        return currentDateStr;
    }

    // 时间字符串转化为时间戳
    + (NSString *)transToTimestampWithString:(NSString *)timeStr FormatStr:(NSString *)formatStr
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeZone:[NSTimeZone localTimeZone]];
        [formatter setDateFormat:formatStr];

        NSDate *date = [formatter dateFromString:timeStr];
      NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
        return timeSp;
    }

    // 将一种格式的时间转化为另一种格式的时间
    + (NSString *)transToTimestampWithSourceStr:(NSString *)sourceStr SourceFormatStr:(NSString *)sourceFormatStr AimFormateStr:(NSString *)aimFormatStr
    {
        NSString *timestampStr = [self transToTimestampWithString:sourceStr FormatStr:sourceFormatStr];
        return [self transToStringWithTimestamp:[timestampStr longLongValue]         FormatStr:aimFormatStr];
    }

    @end

你可能感兴趣的:(无标题文章)