//
// NSString+TimeTransform.m
// WithpathNew
//
// Created by 雨逍 on 2017/1/3.
// Copyright © 2017年 刘干. All rights reserved.
//
import "NSString+TimeTransform.h"
@implementation NSString (TimeTransform)
-(NSString )timeTransform
{
/
0~1分钟 刚刚
1~59分钟 XX分钟前
1~23小时
if 未隔天
XX 小时前
eles
昨天 XX:XX
前天 XX:XX
XX:XX:XX
*/
NSString * infoStr = @"";
NSString * nowStr = getCurentTime;
NSTimeInterval nowTime = [nowStr doubleValue];
NSDate * nowDetaildate = [NSDate dateWithTimeIntervalSince1970:nowTime];
//实例化一个NSDateFormatter对象
NSDateFormatter * nowYear = [[NSDateFormatter alloc] init];
[nowYear setDateFormat:@"yyyy"];
NSDateFormatter * nowMonth = [[NSDateFormatter alloc] init];
[nowMonth setDateFormat:@"MM"];
NSDateFormatter * nowDay = [[NSDateFormatter alloc] init];
[nowDay setDateFormat:@"dd"];
NSDateFormatter * nowHour = [[NSDateFormatter alloc] init];
[nowHour setDateFormat:@"HH"];
NSDateFormatter * nowMin = [[NSDateFormatter alloc] init];
[nowMin setDateFormat:@"mm"];
NSDateFormatter * nowSec = [[NSDateFormatter alloc] init];
[nowSec setDateFormat:@"ss"];
NSString * nowYearStr = [nowYear stringFromDate: nowDetaildate];
NSString * nowMonthStr = [nowMonth stringFromDate: nowDetaildate];
NSString * nowDayStr = [nowDay stringFromDate: nowDetaildate];
// NSString * nowHourStr = [nowHour stringFromDate: nowDetaildate];
// NSString * nowMinStr = [nowMin stringFromDate: nowDetaildate];
// NSString * nowSecStr = [nowSec stringFromDate: nowDetaildate];
NSTimeInterval inputTime = [self doubleValue];
NSDate * detaildate = [NSDate dateWithTimeIntervalSince1970:inputTime];
//实例化一个NSDateFormatter对象
NSDateFormatter * year = [[NSDateFormatter alloc] init];
[year setDateFormat:@"yyyy"];
NSDateFormatter * month = [[NSDateFormatter alloc] init];
[month setDateFormat:@"MM"];
NSDateFormatter * day = [[NSDateFormatter alloc] init];
[day setDateFormat:@"dd"];
NSDateFormatter * hour = [[NSDateFormatter alloc] init];
[hour setDateFormat:@"HH"];
NSDateFormatter * min = [[NSDateFormatter alloc] init];
[min setDateFormat:@"mm"];
NSDateFormatter * sec = [[NSDateFormatter alloc] init];
[sec setDateFormat:@"ss"];
NSString * yearStr = [year stringFromDate: detaildate];
NSString * monthStr = [month stringFromDate: detaildate];
NSString * dayStr = [day stringFromDate: detaildate];
NSString * hourStr = [hour stringFromDate: detaildate];
NSString * minStr = [min stringFromDate: detaildate];
// NSString * secStr = [sec stringFromDate: detaildate];
//同一天
if ([yearStr isEqualToString:nowYearStr] && [monthStr isEqualToString:nowMonthStr] && [dayStr isEqualToString:nowDayStr])
{
if (nowTime - inputTime < 60)
{
//刚刚
infoStr = @"刚刚";
}else if (nowTime - inputTime < 3600)
{
infoStr = [NSString stringWithFormat:@"%d分钟前",([nowStr intValue] - [self intValue])/60];
}else if (nowTime - inputTime < 3600 * 24)
{
infoStr = [NSString stringWithFormat:@"%d小时前",([nowStr intValue] - [self intValue])/3600];
}
return infoStr;
}
//同一月
if ([yearStr isEqualToString:nowYearStr] && [monthStr isEqualToString:nowMonthStr])
{
if ([nowDayStr integerValue] - [dayStr integerValue] == 1)
{
infoStr = [NSString stringWithFormat:@"昨天 %@:%@",hourStr,minStr];
}else if ([nowDayStr integerValue] - [dayStr integerValue] == 2)
{
infoStr = [NSString stringWithFormat:@"前天 %@:%@",hourStr,minStr];
}else
{
infoStr = [NSString stringWithFormat:@"%@-%@",monthStr,dayStr];
}
return infoStr;
}
//同年
if ([yearStr isEqualToString:nowYearStr])
{
infoStr = [NSString stringWithFormat:@"%@-%@",monthStr,dayStr];
return infoStr;
}
infoStr = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dayStr];
// NSTimeInterval inTime = [self doubleValue];
// NSDate * detaildate = [NSDate dateWithTimeIntervalSince1970:inTime];
// //实例化一个NSDateFormatter对象
// NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSString * currentDateStr = [dateFormatter stringFromDate: detaildate];
// NSLog(@"时间: %@",currentDateStr);
return infoStr;
}
@end