Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:04:57+00:00 2026-06-04T01:04:57+00:00

For iOS, has anyone created NSDate extensions for commonly used date-related operations? What I’ve

  • 0

For iOS, has anyone created NSDate extensions for commonly used date-related operations? What I’ve done so far is shown below. I can imagine a lot more here.

#import <Foundation/Foundation.h>

@interface NSDate (Utilities)

// daysOffsetBy -- denotes how many days to offset the current date
// before calculating midnight.  Here are some examples of how this
// parameter works:
//
//   - if 0, then get the next immediate midnight.
//   - if -1, then get yesterday's midnight (ie, early morning today).
//   - if +1, then get tomorrow's mignight.

- (NSDate *)midnight:(NSInteger)daysOffsetBy;

// Use this method to transpose a time-of-day, specified in self,
// to an arbitrary reference date, specified in refDate.  For example,
// suppose the time-of-day in self is 10:30 AM, and the calendar date
// is 1995-07-21.  Suppose further that the calendar date for refDate
// is 2012-05-22, and that the time-of-day for refDate is 17:00 PM.
// This method creates a new date:  2012-05-22 10:30 AM.

- (NSDate *)transposeToDate:(NSDate *)refDate;

// Return YES if the 

- (BOOL) occursInside:(NSDate *)fromTime throughTime:(NSDate *)toTime;

@end

with the corresponding implementation:

#import "NSDate+Utilities.h"

static double SECONDS_IN_DAY = 24*60*60;

@implementation NSDate (Utilities)

- (NSDate *)midnight:(NSInteger)daysOffsetBy {
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDate *refDate = [NSDate dateWithTimeInterval:(SECONDS_IN_DAY * daysOffsetBy) sinceDate:self];
    NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
                                                          NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:refDate];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    NSDate *retval = [gregorian dateFromComponents:components];
    return retval;
}

- (NSDate *)transposeToDate:(NSDate *)refDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self];
    double secondsTranspiredInDay = [components second] + [components minute] * 60 + [components hour] * 60 * 60;

    NSDate *retDate = [NSDate dateWithTimeInterval:secondsTranspiredInDay sinceDate:[refDate midnight:0]];
    return retDate;
}

- (BOOL) occursInside:(NSDate *)fromTime throughTime:(NSDate *)toTime {
    BOOL retval = NO;
    NSComparisonResult c0 = [self compare:fromTime];
    NSComparisonResult c1 = [self compare:toTime];
    if ((c0 == NSOrderedSame || c0 == NSOrderedDescending) && (c1 == NSOrderedSame || c1 == NSOrderedAscending)) {
        retval = YES;
    }
    return retval;
}

@end

Some extras that come to mind are obvious: addDays, addMinutes, addSeconds, etc. Also helpful would be for NSDate-formatters to be integrated into this category.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:04:58+00:00Added an answer on June 4, 2026 at 1:04 am

    Yes, quite a few people have built categories on NSDate.

    You can find a number of them by searching github: https://github.com/search?langOverride=&q=nsdate&repo=&start_value=1&type=Repositories

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone had luck converting and using jpeg2000 on the ios? I am writing
Has anyone done any investigation as to whether the Apple AppStore distribution private key/certificate
Has anyone had problems with pinching in the iOS simulator on a UITableView? I
Has anyone worked with Salesforce mobilesdk for iOS and implemented push? Need some guidance?
Has anyone found a good way to test Google Analytics on iOS? The SDK
I have created an NSDate category that would give me a last sunday date.
Has anyone integrated google docs into their iOS app? Having gone through the example
Has anyone played an ogg stream from iOS? What can I use without having
Has anyone had any luck installing Xcode 3.2.3 + iOS 4.0.2 SDK? I've tried
Has anyone found a halfway decent guide to implementing Reachability on iOS?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.