Home > NSDate > A Category on NSDate
A Category on NSDate
Posted on Sunday, July 12, 2009 by android apps market for tablests
Here is a category on NSDate that makes a few somewhat common tasks that require several lines of code and turns them into a single method all. Among these methods are one that takes a date with a datetime value and turns it into a date without time, a method that calculates a new dates that is a certain number of days after the date, and a method that calculates the difference between two dates given in days.
Nothing earth-shattering, but may save you a few lines of code here and there.
NSDate-Misc.h
NSDate-Misc.m
Nothing earth-shattering, but may save you a few lines of code here and there.
NSDate-Misc.h
#import <Foundation/Foundation.h>
@interface NSDate(Misc)
+ (NSDate *)dateWithoutTime;
- (NSDate *)dateByAddingDays:(NSInteger)numDays;
- (NSDate *)dateAsDateWithoutTime;
- (int)differenceInDaysTo:(NSDate *)toDate;
- (NSString *)formattedDateString;
- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat;
@endNSDate-Misc.m
#import "NSDate-Misc.h"
@implementation NSDate(Misc)
+ (NSDate *)dateWithoutTime
{
return [[NSDate date] dateAsDateWithoutTime];
}
-(NSDate *)dateByAddingDays:(NSInteger)numDays
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:numDays];
NSDate *date = [gregorian dateByAddingComponents:comps toDate:self options:0];
[comps release];
[gregorian release];
return date;
}
- (NSDate *)dateAsDateWithoutTime
{
NSString *formattedString = [self formattedDateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd, yyyy"];
NSDate *ret = [formatter dateFromString:formattedString];
[formatter release];
return ret;
}
- (int)differenceInDaysTo:(NSDate *)toDate
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSDayCalendarUnit
fromDate:self
toDate:toDate
options:0];
NSInteger days = [components day];
[gregorian release];
return days;
}
- (NSString *)formattedDateString
{
return [self formattedStringUsingFormat:@"MMM dd, yyyy"];
}
- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:dateFormat];
NSString *ret = [formatter stringFromDate:self];
[formatter release];
return ret;
}
@endCategory Article Categories, Cocoa, Cocoa Touch, NSDate
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
July
(61)
- We're Listening
- We're Listening
- The Iterative Web App: Links Got Shorter and Smarter
- The Iterative Web App: Links Got Shorter and Smarter
- DP aka Dataran Pahlawan Malacca.
- The Most Reliable
- Google Latitude. Now for iPhone.
- Google Latitude. Now for iPhone.
- Interesting Contrast
- Waving the Red Flag
- Microsoft's Ads Redux
- Layers of fun in Google Maps for mobile 3.2
- Layers of fun in Google Maps for mobile 3.2
- August 14 Workshop Update
- To VBO or Not to VBO...
- Improved Blender Export
- Refactoring Nav from Chapter 9
- Voyager Class Mixmaster
- Fixed & Improved Blender Export Script
- How MJ Died..
- A Little Ego Boost for a Friday
- WWDC 2009 Videos Available for Purchase
- Beginning iPhone 3 Development is Available
- Gah! Up is Down! Right is Wrong! Make it Stop!
- First Post
- Search with My Location for iPhone 3.0. And All t...
- Search with My Location for iPhone 3.0. And All t...
- Browse a collection of experts' favorite places us...
- Browse a collection of experts' favorite places us...
- New Device Rules
- Updating Project Hint
- Google Voice for Android and BlackBerry
- Google Voice for Android and BlackBerry
- Detail Editor Update: To Many Relationships
- Generic Detail Editor Update
- August 14 Workshop
- A Category on NSDate
- unusual business ideas that work Gadget
- Core Data Default Dates in Data Model
- A Kick-Ass Objective-C Programmer is a Kick-Ass C ...
- TinyPlayground Gadget
- Google Product Search for mobile now available for...
- Google Product Search for mobile now available for...
- Travelzoo UK Deals
- In Search of a Better Way: Editable Detail Views
- The Perils of Helping Others
- Core Plot
- More on Teaching
- An IPhone App Setback
- Oh, Good, Here Comes the Justice Department
- Windows Mobile: World Domination is In Sight!
- Core Data Navigation-Based Application
- iPhone Simulator Application Creator
- Wireless Carriers the Great Train Robbers of this ...
- An Exercise in Blatant Bias
- Core Data - Inserting a New Managed Object
- Google Search results optimized for feature phones...
- Google Search results optimized for feature phones...
- What, Me Teach?
- Mint Apps Promo Day is Today!
- Core Data - Determining if a Managed Object is New
-
▼
July
(61)