// // KXTime.m // CAISHEN // // Created by kuxuan on 2017/8/25. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXTime.h" @implementation KXTime + (BOOL)isToday:(NSDate *)date { if ([[self getFormateDate:date] isEqualToString:@"今天"]) { return YES; }else{ return NO; } } + (NSString *)getFormateDate:(NSDate *)newsDate { NSTimeInterval secondsPerDay = 24 * 60 * 60 * 1000; NSDate *today=[NSDate date]; NSDate *yearsterDay = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay]; NSDate *qianToday = [[NSDate alloc] initWithTimeIntervalSinceNow:-2*secondsPerDay]; //假设这是你要比较的date:NSDate *yourDate = …… //日历 NSCalendar* calendar = [NSCalendar currentCalendar]; unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour; NSDateComponents* comp1 = [calendar components:unitFlags fromDate:newsDate]; NSDateComponents* comp2 = [calendar components:unitFlags fromDate:yearsterDay]; NSDateComponents* comp3 = [calendar components:unitFlags fromDate:qianToday]; NSDateComponents* comp4 = [calendar components:unitFlags fromDate:today]; if ( comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day) { return @"昨天"; } if (comp1.year == comp3.year && comp1.month == comp3.month && comp1.day == comp3.day) { return @"前天"; } if (comp1.year == comp4.year && comp1.month == comp4.month && comp1.day == comp4.day) { return @"今天"; } return @"0"; } + (BOOL)validateWithStartTime:(NSString *)startTime withExpireTime:(NSString *)expireTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm"]; NSString *today = [dateFormat stringFromDate:now]; NSDate *start = [dateFormat dateFromString:[today stringByReplacingCharactersInRange:NSMakeRange(11, 5) withString:startTime]]; NSDate *expire = [dateFormat dateFromString:[today stringByReplacingCharactersInRange:NSMakeRange(11, 5) withString:expireTime]]; if ([now compare:start] == NSOrderedDescending && [now compare:expire] == NSOrderedAscending) { return YES; } return NO; } @end