Skip to content
This repository

Methods

@interface NSDate (Helper)

- (NSUInteger)daysAgo;
- (NSUInteger)daysAgoAgainstMidnight;
- (NSString *)stringDaysAgo;
- (NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag;
- (NSUInteger)weekday;
- (NSDate *)beginningOfWeek;
- (NSDate *)beginningOfDay;
- (NSDate *)endOfWeek; // nodoc, should be obvious...

+ (NSString *)dbFormatString;
+ (NSDate *)dateFromString:(NSString *)string;
+ (NSString *)stringFromDate:(NSDate *)date;
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)string;
+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed;
+ (NSString *)stringForDisplayFromDate:(NSDate *)date;

@end

- (NSUInteger)daysAgo;

Returns the number of days that have expired between now and the NSDate in question, based on NSComponent calculation.

e.g. [someDate daysAgo]

- (NSUInteger)daysAgoAgainstMidnight;

This is a better version of daysAgo:, works off of midnight for calculating the days since a date.

- (NSString *)stringDaysAgo;

Returns a handy string for UI displays, e.g. the UIKit Notes application: ‘Today’, ‘Yesterday’, or ‘N days ago’. Not localized. Calls stringDaysAgoAgainstMidnight:YES to calculate the number of days.

- (NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag;

Same as stringDaysAgo, unless the user supplies NO for the flag parameter. In this case, NSComponent is consulted to determine the number of days since the date in question.

- (NSUInteger)weekday;

Usual output ranges from 1 (for Sunday) to 7 (for Saturday). Returns the weekday component of a date when analyzed by NSComponent via [NSCalendar currentCalendar]. Therefore, it doesn’t ensure a Gregorian calendar, and you may want to keep this in mind if you’re using it in locales where there are more (or less) than seven days to a week.

- (NSDate *)beginningOfDay;

Uses the default calendar to return a normalized version of the date to midnight,
e.g. [[NSdate date] beginningOfDay].

- (NSDate *)beginningOfWeek;

Returns the date for the beginning of the week, normalized to midnight,
e.g. [[NSDate date] beginningOfWeek].

+ (NSString *)dbFormatString;

Returns "yyyy-MM-dd HH:mm:ss". There’s not much to this, but it can be very useful. Instead of hand-coding your DB format strings all over your app, call a method to get the format string. Later if you need to change it, you only have to change it in one place.

+ (NSDate *)dateFromString:(NSString *)string;

Returns an NSDate object by reading the given string with a date formatter, using the [NSDate dbFormatString] method documented above. This means that string should hold a date in the format "yyyy-MM-dd HH:mm:ss". Useful for database operations.

+ (NSString *)stringFromDate:(NSDate *)date;

Returns a "yyyy-MM-dd HH:mm:ss" formatted string from the given NSDate. Nice for database operations.

+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)string;

I have a real loathing or NSDateFormatter and have no desire to use it for simple stuff. This method allows you to get a formatted string representation of a date object in whatever format you like, specified in the withFormat param. No need for you to create the extra objects.

+ (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed;

This is the crown jewel method here (imho). It produces the following kinds of output based on an NSDate object:

  • ‘3:42 AM’ – if the date is after midnight today
  • ‘Tuesday’ – if the date is within the last seven days
  • ‘Mar 1’ – if the date is within the current calendar year
  • ‘Mar 1, 2008’ – else ;-)

This method could probably use to be optimized, but I did try to code it somewhat sensibly. The prefixed boolean option will prepend the string ‘on’ to the output string, e.g. 'on Tuesday'. Not localized.

+ (NSString *)stringForDisplayFromDate:(NSDate *)date;

This is a short-cut method, calls stringForDisplayFromDate:prefixed: with the prefixed flag set to NO, to preserve the original behavior.

Something went wrong with that request. Please try again.