标签:
直接看代码吧:
// Created by cctv on 15/8/7. // Copyright (c) 2015年 cctv. All rights reserved. //
#import "NSString+add.h" #import "NSDate+add.h"
@implementation NSString (add) + (NSString *)stringWithDateString:(NSString*) dateString { NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; dateF.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; dateF.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-AS"]; [dateF dateFromString:dateString]; // 创建时间 NSDate *date = [dateF dateFromString:dateString]; //判断创建时间与当前时间的关系 if (date.dateIsThisDay) { NSDateComponents *dateComp = [date dateIsToday]; if (dateComp.hour >= 1) { dateF.dateFormat = [NSString stringWithFormat:@"%d小时前",dateComp.hour]; return [dateF stringFromDate:date]; }else if(dateComp.minute >= 1 ){ dateF.dateFormat = [NSString stringWithFormat:@"%d分钟前",dateComp.minute]; return [dateF stringFromDate:date]; }else{ return @"刚刚"; } }else if (date.dateIsYesterDay){ dateF.dateFormat = @"昨天 HH:mm"; return [dateF stringFromDate:date]; }else if(date.dateIsThisYear){ dateF.dateFormat = @"MM-dd HH:mm"; return [dateF stringFromDate:date]; }else{ dateF.dateFormat = @"yyyy-MM-dd HH:mm"; return [dateF stringFromDate:date]; } }
@end --------。。。。。------
#import "NSDate+add.h"
@implementation NSDate (add) - (BOOL)dateIsThisYear { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear) fromDate:[NSDate date]]; NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear) fromDate:self]; return nowComp.year == selfComp.year; } - (BOOL)dateIsThisDay { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]]; NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self]; return nowComp.year == selfComp.year && nowComp.month == selfComp.month && nowComp.day == selfComp.day; } - (BOOL)dateIsYesterDay { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComp = [calendar components:(NSCalendarUnitYear) fromDate:[NSDate date]]; NSDateComponents *selfComp = [calendar components:(NSCalendarUnitYear) fromDate:self]; return nowComp.year == selfComp.year; } - (NSDateComponents*)dateIsToday { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateComp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:self toDate:[NSDate date] options:0]; return dateComp; }
@end
|
标签:
原文地址:http://www.cnblogs.com/qq449832375/p/4719371.html