若你只是想把绝对日期字符串(至1970到现在的毫秒数)转化为年月日,时分秒,可以建立一个分类,使用系统函数自己转化。不需要我上篇文章说的完全自己实现日期转化函数。
注意:
服务器默认记路的日期字符串是精确的毫秒,苹果是默认精确到秒。
调用例子:

"%lld", model.startPoint.obtainTime*1000];
_dateLabel.text = [time dateFomatterStringWithMD];

NSString+Extension.h

#import <Foundation/Foundation.h>

@interface NSString (Extension)
/**
* 把时间戳格式化为 MM月dd日 EEE HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithMDEHM;

/**
* 把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithYMDHM;
/**
* 把时间戳格式化为 yyyy.MM.dd HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithymdhm;
/**
* 把时间戳格式化为 MM月dd日 HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithMDHM;
/**
* 把时间戳格式化为 HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithHM;
/**
* 把时间戳格式化为今天(*日) HH:mm 的格式
*/
- (NSString *)dateFomatterStringWithDayHM;
/**
* 把时间戳格式化为 MM月dd日的格式
*/
- (NSString *)dateFomatterStringWithMD;
/**
* 把时间戳格式化为 yyyy-MM-dd 的格式
*/
- (NSString *)dateFomatterStringWithYMD;
@end

NSString+Extension.m

#import "NSString+Extension.h"

@implementation NSString (Extension)

#pragma mark - 把时间戳格式化为 MM月dd日 EEE HH:mm 的格式
- (NSString *)dateFomatterStringWithMDEHM;
{
FLDDLogDebug(@"函数");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM月dd日 EEE HH:mm"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式
- (NSString *)dateFomatterStringWithYMDHM;
{
FLDDLogDebug(@"函数");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy.MM.dd HH:mm 的格式
- (NSString *)dateFomatterStringWithymdhm
{
FLDDLogDebug(@"函数");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy.MM.dd HH:mm"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 yyyy的格式
- (NSString *)dateFomatterStringWithy
{
FLDDLogDebug(@"函数");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 MM月dd日 HH:mm 的格式
- (NSString *)dateFomatterStringWithMDHM {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM月dd日 HH:mm"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}


#pragma mark - 把时间戳格式化为 MM月dd日的格式
- (NSString *)dateFomatterStringWithMD {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM月dd日"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
#pragma mark - 把时间戳格式化为 HH:mm 的格式
- (NSString *)dateFomatterStringWithHM {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"HH:mm"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}

#pragma mark - 把时间戳格式化为今天(*日) HH:mm 的格式
- (NSString *)dateFomatterStringWithDayHM {
long long nowTime = (long long)([[NSDate date] timeIntervalSince1970] * 1000);
if([self longLongValue] < 100000)
{
return @"未知时间";
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if(ABS(nowTime - [self longLongValue]) > 3600000)
{
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"dd日"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
NSString *dayStr = [formatter stringFromDate:confromTimesp];

formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"HH:mm"];
confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr];
}
else
{
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"dd日"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
NSString *dayStr = [formatter stringFromDate:confromTimesp];

confromTimesp = [NSDate dateWithTimeIntervalSince1970:nowTime/1000];
NSString *todayStr = [formatter stringFromDate:confromTimesp];
if([dayStr isEqualToString: todayStr])
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"HH:mm"];
confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
return [[NSString alloc] initWithFormat:@"今天 %@",hourMinuteStr];
}
else
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"HH:mm"];
confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp];
return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr];
}

}

}

/**
* 把时间戳格式化为 yyyy-MM-dd 的格式
*/
- (NSString *)dateFomatterStringWithYMD;
{
if([self longLongValue] < 100000)
{
return @"未知时间";
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000];
return [formatter stringFromDate:confromTimesp];
}
@end