• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Calculate next occurrence of a NSDate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calculate next occurrence of a NSDate


  • Subject: Re: Calculate next occurrence of a NSDate
  • From: WT <email@hidden>
  • Date: Fri, 31 Dec 2010 11:22:35 -0200

Hey Tom,

as it happens, I just did this a few weeks ago for my current project. The following is straight out of my code, and computes the next Wed or Sat from a given date.

Enjoy!
WT

- (NSDate*) nextWedOrSatFromDate: (NSDate*) date;
{
    NSCalendar* cal = [[NSCalendar alloc]
        initWithCalendarIdentifier: NSGregorianCalendar];

    NSDateComponents* comps = [cal components: NSWeekdayCalendarUnit
                                     fromDate: date];

    // [comps weekday] gives Sun = 1, Mon = 2, etc, Sat = 7,
    // so take mod 7 to make Sat = 0.

    NSInteger weekday = [comps weekday] % 7;

    // Sat = 0, Sun = 1, Mon = 2, Tue = 3,
    // Wed = 4, Thu = 5, Fri = 6

    if (weekday < 4) // it's Sat or beyond, but before Wed
    {
        // add enough full days to get to Wed

        NSInteger secs = (4 - weekday) * 24 * 60 * 60;
        date = [date dateByAddingTimeInterval: secs];
    }
    else // it's beyond Tue but before Sat
    {
        // add enough full days to get to Sat

        NSInteger secs = (7 - weekday) * 24 * 60 * 60;
        date = [date dateByAddingTimeInterval: secs];
    }

    [cal release];

    return date;
}

On Dec 31, 2010, at 2:03 AM, Tom Jones wrote:

> Hello,
> I'm trying to calculate next occurrence of a NSDate object. Based on a "Start Date" ( e.g. 2010-12-10, Friday ) I would like to know the date of the next Friday occurrence from today ( e.g. 2010-12-30, Thursday ).
>
> Example:
> StartDate = 2010-12-10
> CurrentDate = 2010-12-30
> Next Occurrence = 2010-12-31 <-Answer
>
> I have not done this before so I'm not sure how to tackle this.
>
> Thanks,
> tom
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Calculate next occurrence of a NSDate
      • From: Ken Thomases <email@hidden>
References: 
 >Calculate next occurrence of a NSDate (From: Tom Jones <email@hidden>)

  • Prev by Date: NSTableHeaderView, how to clear title before resetting new title
  • Next by Date: UITableViewCell showsReorderControl does nothing
  • Previous by thread: Re: Calculate next occurrence of a NSDate
  • Next by thread: Re: Calculate next occurrence of a NSDate
  • Index(es):
    • Date
    • Thread