Sin descripción

KXTime.m 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // KXTime.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/8/25.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXTime.h"
  9. @implementation KXTime
  10. + (BOOL)isToday:(NSDate *)date
  11. {
  12. if ([[self getFormateDate:date] isEqualToString:@"今天"]) {
  13. return YES;
  14. }else{
  15. return NO;
  16. }
  17. }
  18. + (NSString *)getFormateDate:(NSDate *)newsDate
  19. {
  20. NSTimeInterval secondsPerDay = 24 * 60 * 60 * 1000;
  21. NSDate *today=[NSDate date];
  22. NSDate *yearsterDay = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];
  23. NSDate *qianToday = [[NSDate alloc] initWithTimeIntervalSinceNow:-2*secondsPerDay];
  24. //假设这是你要比较的date:NSDate *yourDate = ……
  25. //日历
  26. NSCalendar* calendar = [NSCalendar currentCalendar];
  27. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour;
  28. NSDateComponents* comp1 = [calendar components:unitFlags fromDate:newsDate];
  29. NSDateComponents* comp2 = [calendar components:unitFlags fromDate:yearsterDay];
  30. NSDateComponents* comp3 = [calendar components:unitFlags fromDate:qianToday];
  31. NSDateComponents* comp4 = [calendar components:unitFlags fromDate:today];
  32. if ( comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day) {
  33. return @"昨天";
  34. }
  35. if (comp1.year == comp3.year && comp1.month == comp3.month && comp1.day == comp3.day)
  36. {
  37. return @"前天";
  38. }
  39. if (comp1.year == comp4.year && comp1.month == comp4.month && comp1.day == comp4.day)
  40. {
  41. return @"今天";
  42. }
  43. return @"0";
  44. }
  45. + (BOOL)validateWithStartTime:(NSString *)startTime withExpireTime:(NSString *)expireTime {
  46. NSDate *now = [NSDate date];
  47. NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  48. [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
  49. NSString *today = [dateFormat stringFromDate:now];
  50. NSDate *start = [dateFormat dateFromString:[today stringByReplacingCharactersInRange:NSMakeRange(11, 5) withString:startTime]];
  51. NSDate *expire = [dateFormat dateFromString:[today stringByReplacingCharactersInRange:NSMakeRange(11, 5) withString:expireTime]];
  52. if ([now compare:start] == NSOrderedDescending && [now compare:expire] == NSOrderedAscending) {
  53. return YES;
  54. }
  55. return NO;
  56. }
  57. @end