Skip to content

Instantly share code, notes, and snippets.

@gavingmiller
Created December 23, 2010 01:19
Show Gist options
  • Save gavingmiller/752403 to your computer and use it in GitHub Desktop.
Save gavingmiller/752403 to your computer and use it in GitHub Desktop.
//
// NSDate+Surrounding.h
//
// Created by Josh Kendall on 12/22/10.
// Copyright 2010 JoshuaKendall.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDate (Surrounding)
- (NSDate *)dateSinceToday:(int)days;
- (NSDate *)yesterday;
- (NSDate *)tomorrow;
@end
//
// NSDate+Surrounding.m
//
// Created by Josh Kendall on 12/22/10.
// Copyright 2010 JoshuaKendall.com. All rights reserved.
//
#import "NSDate+Surrounding.h"
@implementation NSDate (Surrounding)
- (NSDate *)dateSinceToday:(int)days {
NSDate *date = [NSDate date];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:days];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
return [gregorian dateByAddingComponents:components toDate:date options:0];
}
- (NSDate *)yesterday {
return [self dateSinceToday: -1];
}
- (NSDate *)tomorrow {
return [self dateSinceToday: 1];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment