美文网首页
时间与时间戳

时间与时间戳

作者: CHADHEA | 来源:发表于2017-03-10 11:34 被阅读0次

    //.h

    //时间转date

    + (NSDate *)dataWithTime:(NSString *)time;

    //时间转时间戳

    + (NSString *)timeStampWithTime:(NSString *)time;

    //时间戳转时间

    + (NSString *)timeWithTimeStamp:(NSString *)timeStamp;

    //时间戳转时间 + 格式

    + (NSString *)timeWithTimeStamp:(NSString *)timeStamp formatter:(NSString *)formatter;

    //.m

    //时间转data

    + (NSDate *)dataWithTime:(NSString *)time {

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

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"yyyyMMddHHMMss"];

    NSDate *date = [formatter dateFromString:time];

    return date;

    }

    //时间转时间戳

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

    NSDate *date = [self dataWithTime:time];

    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];

    return timeSp;

    }

    //时间戳转时间

    + (NSString *)timeWithTimeStamp:(NSString *)timeStamp {

    return [self timeWithTimeStamp:timeStamp formatter:@"YYYY-MM-dd HH:mm:ss"];

    }

    //时间戳转时间 + 格式

    + (NSString *)timeWithTimeStamp:(NSString *)timeStamp formatter:(NSString *)formatter {

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

    [dataFormatter setDateStyle:NSDateFormatterMediumStyle];

    [dataFormatter setTimeStyle:NSDateFormatterShortStyle];

    [dataFormatter setDateFormat:formatter];

    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:kDouble(timeStamp)];

    NSString *confromTimespStr = [dataFormatter stringFromDate:confromTimesp];

    return confromTimespStr;

    }

    谢谢阅读!

    相关文章

      网友评论

          本文标题:时间与时间戳

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