@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
Returns the number of days that have expired between now and the NSDate in question, based on NSComponent calculation.
This is a better version of
daysAgo:, works off of midnight for calculating the days since a date.
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.
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.
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.
Uses the default calendar to return a normalized version of the date to midnight,
[[NSdate date] beginningOfDay].
Returns the date for the beginning of the week, normalized to midnight,
[[NSDate date] beginningOfWeek].
"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.
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.
"yyyy-MM-dd HH:mm:ss" formatted string from the given NSDate. Nice for database operations.
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.
This is the crown jewel method here (imho). It produces the following kinds of output based on an NSDate object:
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.
This is a short-cut method, calls
stringForDisplayFromDate:prefixed: with the prefixed flag set to
NO, to preserve the original behavior.
Last edited by billymeltdown,