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

  • SEARCH
  • Home
  • 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 9061337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:27:30+00:00 2026-06-16T15:27:30+00:00

Given an arbitrary date, I need to find the date of the first day

  • 0

Given an arbitrary date, I need to find the date of the first day of the next week of the month. Note that it is not as simple as adding 7 days to the current date because the last week of the month may be less than 7 days. Here is the code I’m using now:

    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSWeekOfMonthCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSDayCalendarUnit fromDate:currentDate];

    NSLog(@"week of month: %ld", [components weekOfMonth]);

    [components setWeekOfMonth:[components weekOfMonth] + 1];
    NSLog(@"new week of month: %ld", [components weekOfMonth]); //week of month is now 2

    [components setWeekday:1];
    NSDate *nextWeek = [calendar dateFromComponents:components];

As an example, currentDate is set to 2012-10-01. In this example nextWeek is always 2012-10-01. It appears that sending setWeekOfMonth: does not increment the other date components in the NSDateComponents object. Do I have the wrong date components configured, or is setWeekOfMonth: not supposed to work like that?

  • 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-16T15:27:31+00:00Added an answer on June 16, 2026 at 3:27 pm

    So…. Let’s start with an NSDate and an NSCalendar:

    NSDate *date = ...;
    NSCalendar *cal = [NSCalendar currentCalendar];
    

    Figure out what day of the week that date is:

    NSInteger weekdayOfDate = [cal ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date];
    NSInteger numberOfDaysToStartOfCurrentWeek = weekdayOfDate - 1;
    

    Let’s move that date into the next week:

    NSDateComponents *oneWeek = [[NSDateComponents alloc] init];
    [oneWeek setWeek:1]; // add one week
    [oneWeek setDay:-numberOfDaysToStartOfCurrentWeek]; // ... and subtract a couple of days to get the first day of the week
    NSDate *startOfNextWeek = [cal dateByAddingComponents:oneWeek toDate:date options:0];
    

    At this point, you have a date that points to the first day of the next week. Now we should verify that it’s still in the same month:

    NSDateComponents *monthOfNextWeek = [cal components:NSMonthCalendarUnit fromDate:startOfNextWeek];
    NSDateComponents *monthOfThisWeek = [cal components:NSMonthCalendarUnit fromDate:date];
    if ([monthOfNextWeek month] != [monthOfThisWeek month]) {
      // the first day of the next week is not in the same month as the start date
    }
    

    When I run this, I get that the start of next week is 30 Dec 2012 (a Sunday, because my calendar’s weeks start on Sunday).

    If, however, I want the first day of the week to start on Monday, I can preface this code with:

    [cal setFirstWeekday:2]; // the first day of the week is the second day (ie, Monday, because Sunday = 1)
    

    If I do this, then the startOfNextWeek results in 31 Dec 2012.

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

Sidebar

Related Questions

Given an arbitrary 4x4 transformation matrix, how do I find out the center of
Given four (x,y) pairs that represent the four corners of an arbitrary polygon (quadrilateral)
I need a counter algortihm which use arbitrary given digits for counting purpose. My
The scenario is as follows: given a totally arbitrary starting date in UTC there
Given an arbitrary file descriptor, can I make it blocking if it is non-blocking?
Given some arbitrary SQL I would like to get the data types of the
Basically i just want to do an arbitrary operation using given arguments of arbitrary
Given two planes positioned and rotated arbitrary to each other, I'm trying to determine
You're given an array of size n, containing arbitrary boolean values. What is the
Given that the web application doesn't have su privileges, I'd like to execute a

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.