7

I want to get the date of the next Monday after the current date.

So if today's date is 2013-08-09 (Friday) then I want to get the date 2013-08-12.

How can I do this?

jscs
  • 63,694
  • 13
  • 151
  • 195
ChandreshKanetiya
  • 2,414
  • 5
  • 27
  • 41
  • 5
    Although this is a simple question and requires little effort, dates are annoying as hell in any language so i don't see why you should be down-voted. – A'sa Dickens Aug 09 '13 at 14:58

2 Answers2

33

This piece of code should get what you want. It simply calculates how many days are from monday and append it from current's date.

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit fromDate:now];

NSUInteger weekdayToday = [components weekday];  
NSInteger daysToMonday = (9 - weekdayToday) % 7;

NSDate *nextMonday = [now dateByAddingTimeInterval:60*60*24*daysToMonday];

Untested, but should work, and without worrying about changing first dates of calendar.

And it can even be easily addapted to every another day of the week, just change the 9inside (9 - weekdayToday) % 7; by 7 + weekDayYouWant, remembering that sunday = 1, monday = 2...

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • Thanks Lucas , works perfectly. Just one thing , if today is Monday then I want to show next Monday instead of this Monday. Can you tell me how can I achieve this ? Thanks again – iOSAppDev Jun 10 '14 at 11:52
  • I don't think there's a elegant way to change the behaviour for just one day like that. But you can **add** (not replace) something like that, **before** the ```nextMonday``` code: ```daysToMonday = (daysToMonday == 0) ? 7 : daysToMonday;``` . In this way, you would be adding 7 days instead of 0 days when the current day is monday. If you find a better solution for this case, just let me know – Lucas Eduardo Jun 10 '14 at 12:06
  • 3
    won't work for daylight savings time. See this for modified solution for adding the days http://stackoverflow.com/questions/5067785/how-do-i-add-1-day-to-a-nsdate – Josh Woodcock Jun 12 '15 at 02:43
  • 1
    Since macOS 10.9 / iOS 8 the recommended way is `nextDayAfterDate:matchingComponents:options:` of `NSCalendar`. It can consider daylight saving changes all over the world and is much more reliable than `86400`. – vadian Jun 06 '17 at 12:09
4

You can use NSCalendar method dateFromComponents: passing a properly initiated NSDateComponents object

NSDateComponents *components = [[NSCalendar currentCalendar] components: NSYearCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setWeekOfYear:[components weekOfYear] + 1];
[comps setWeekday:1];
[comps setYear:[components year]];
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2]; //This needs to be checked, which day is monday?
NSDate *date = [calendar dateFromComponents:comps];

Something along these lines could work (blindly typed)

Vik
  • 1,897
  • 12
  • 18
  • assuming the week begins in sunday (week day = 0), wouldn't this code get the monday of the week after instead of only the day after when running on sundays? – Lucas Eduardo Aug 09 '13 at 14:07
  • 3
    Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1. So yes, probably the code needs to be changed – Vik Aug 09 '13 at 14:10
  • 1
    I slightly edited the code to take this into account. Of course one should also check that the current day isn't the last week of the year and so on.. – Vik Aug 09 '13 at 14:13
  • @Vik its not work, but i like that approach. – Evgeniy Kleban May 12 '17 at 11:44