-1

I want a method which just returning me the date of next fixed name day.(ex: Monday, Thursday, etc).

Ex:So if I want to give a name(monday) or a number (1, first day from week), it should returning 20/07/2015.

Is it possible to get?

EDITED:

I want to take most appropriated day(Monday,Thursday, Saturday, etc) by my current date.

I give you a example. Current date is WED, 15/07/15:

If I want to take most appropriated date Friday, the result is should be date:17/07/15.

If I want to take most appropriated date Monday, the result is should be date:20/07/15.

How can I get this?

user3182143
  • 9,459
  • 3
  • 32
  • 39
Gaby Fitcal
  • 1,814
  • 1
  • 17
  • 28
  • Could not understood your question , may you get help from this : http://stackoverflow.com/a/4269211/3202193 – Ashish Kakkad Jul 15 '15 at 09:11
  • there are 52 Mondays in a year and additionally there are thousands of years. so, which date would you like to get as _output_ if the _input_ is a simple name of a day? I guess your question and request need to be clarified a bit more as currently it is undetermined and can lead ambiguous answers, which you might downvote at the end of the day, saying "it does not meet your requirement". – holex Jul 15 '15 at 09:21
  • i accept what holex said.Ask clearly – user3182143 Jul 15 '15 at 09:22
  • I edited. I hope you understand now – Gaby Fitcal Jul 15 '15 at 09:34

4 Answers4

2

This will help you get the current day of the week. You can then use that to display the next day or whatever condition you like.

NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];

switch ([component weekday]) {

    case 1: NSLog(@"Sunday"); break;    
    case 2: NSLog(@"Monday"); break;    
    case 3: NSLog(@"Tuesday"); break;   
    case 4: NSLog(@"Wednesday"); break; 
    case 5: NSLog(@"Thursday"); break;
    case 6: NSLog(@"Friday"); break;
    case 7: NSLog(@"Saturday"); break;

    default: NSLog(@"Error..."); break;
}
  • 1
    just the opposite was asked... the OP wants to get the date _from_ a name of a weekday. – holex Jul 15 '15 at 10:20
0

Here it is:

NSDate *now = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:1]; // Here you can give any positive number you want
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:now options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSLog(@"%@", formatter);
Dree
  • 702
  • 9
  • 29
0

as the first weekday can be different for different locale settings, this is a solution using the weekday index. The return value is an optional NSDate object

func dateOfNextWeekday(weekday : Int) -> NSDate? {
  let components = NSDateComponents()
  components.weekday = weekday
  let calendar = NSCalendar.currentCalendar()
  return calendar.nextDateAfterDate(NSDate(), matchingComponents: components, options: .MatchNextTime)
}

dateOfNextWeekday(1)
vadian
  • 274,689
  • 30
  • 353
  • 361
0

In the end i used this code. It works, thank you all!

 NSDate *pickerDate = [NSDate date];

 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;

 NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:pickerDate];
   int weekday = [comps weekday];

 NSDateComponents *dayComponent = [[NSDateComponents alloc] init];

 dayComponent.day = 1;

 while (weekday!=aux) 

 { 

     //aux is number day in week: Sunday=1, Monday=2

     pickerDate = [gregorian dateByAddingComponents:dayComponent toDate:pickerDate options:0];

     comps = [gregorian components:NSWeekdayCalendarUnit fromDate:pickerDate];

     weekday = [comps weekday];

 }
Gaby Fitcal
  • 1,814
  • 1
  • 17
  • 28