时间与时间戳

2017-03-10  本文已影响0人  CHADHEA

//.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;

}

谢谢阅读!

上一篇下一篇

猜你喜欢

热点阅读